使用 "geom" 键获取值的下划线

Underscore getting values with key of "geom"

我有这个对象数组。

[ { geom: '{"type":"Point","coordinates":[-3.81086160022019,50.4619066354793]}' },
  { geom: '{"type":"Point","coordinates":[-4.038333333,51.17166667]}' },
  { geom: '{"type":"Point","coordinates":[-4.286666667,50.99666667]}' },
  { geom: '{"type":"Point","coordinates":[-4.006666667,51.11833333]}' },
  { geom: '{"type":"Point","coordinates":[-3.155,50.75333333]}' } ]

我想要没有 geom:

[ {"type":"Point","coordinates":[-3.81086160022019,50.4619066354793]},
  {"type":"Point","coordinates":[-4.038333333,51.17166667]},
  {"type":"Point","coordinates":[-4.286666667,50.99666667]},
  {"type":"Point","coordinates":[-4.006666667,51.11833333]},
  {"type":"Point","coordinates":[-3.155,50.75333333]}]

这可以用下划线来完成吗?

您也可以在没有 underscore 的情况下执行此操作。您只需要遍历数组和 return currentObj.geom。此外, currentObj.geom 是一个字符串,因此您需要 JSON.parse

var a = [ { geom: '{"type":"Point","coordinates":[-3.81086160022019,50.4619066354793]}' },
  { geom: '{"type":"Point","coordinates":[-4.038333333,51.17166667]}' },
  { geom: '{"type":"Point","coordinates":[-4.286666667,50.99666667]}' },
  { geom: '{"type":"Point","coordinates":[-4.006666667,51.11833333]}' },
  { geom: '{"type":"Point","coordinates":[-3.155,50.75333333]}' } ]


var result = a.map(function(item){
  return JSON.parse(item.geom);
});

document.write("<pre>" + JSON.stringify(result,0,4) + "</pre>");

正如@Rajesh所说,这里不需要下划线,但如果你真的想使用它,那么可以这样做:-

var data = [ { geom: '{"type":"Point","coordinates":[-3.81086160022019,50.4619066354793]}' },
             { geom: '{"type":"Point","coordinates":[-4.038333333,51.17166667]}' },
             { geom: '{"type":"Point","coordinates":[-4.286666667,50.99666667]}' },
             { geom: '{"type":"Point","coordinates":[-4.006666667,51.11833333]}' },
             { geom: '{"type":"Point","coordinates":[-3.155,50.75333333]}' } ];

var vals = _.map(data, function(obj){
   return JSON.parse(obj.geom);
});