【问题标题】:Construct new array of objects from object array [closed]从对象数组构造新的对象数组[关闭]
【发布时间】:2018-04-04 20:05:10
【问题描述】:

我有这个来自 api 的对象数组,包含我需要存储到另一个对象数组的纬度和经度。

response: [
  {
    title: "Map marker 1",
    lat: 10.0,
    lng: 10.0
  },
  {
    title: "Map marker 2",
    lat: 11.0,
    lng: 11.0
  }
]

获取lat 和lng 值并将它们存储到具有以下结构的新对象数组的首选方法是什么?

markers: [
   {
     position: {lat: 10.0, lng: 10.0}
   }, 
   {
     position: {lat: 11.0, lng: 11.0}
   }
]

【问题讨论】:

  • .map(): developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… 另外,在发帖之前至少做一些基础研究...
  • 不,不需要 .map()。
  • @RandyCasburn 需要详细说明吗?
  • 在不必要的时候使用.map(), .reduce() 等有很大的推动力。 Array.prototype.forEach() 非常适合这个用例。在具有大型数据集的大型应用程序中,.map(), .reduce() 等在使用诸如此类的简单数据结构时比传统的 for 循环慢得多。但是什么。
  • 我和@RandyCasburn 在一起,如果你不需要旧的回复,你可以就地修改它(例如:gist.github.com/spelcaster/3029f8c134533ea83fde53c237896bbb)

标签: javascript arrays object


【解决方案1】:

使用Array.prototype.map 可能是最明显的方式。它将允许您将每个数组元素转换为您想要的形状。

let response = [
  {
    title: "Map marker 1",
    lat: 10.0,
    lng: 10.0
  },
  {
    title: "Map marker 2",
    lat: 11.0,
    lng: 11.0
  }
];

let markers = response.map(r => {
  return {
     position: {lat: r.lat, lng: r.lng}
   }
});

console.log(markers);

【讨论】:

    【解决方案2】:

    使用Array.from的替代方法

    var response = [  {    title: "Map marker 1",    lat: 10.0,    lng: 10.0  },  {    title: "Map marker 2",    lat: 11.0,    lng: 11.0  }],
        markers = Array.from(response, ({lat, lng}) => ({position: {lat, lng}}));
    
    console.log(markers);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    最快(性能)的方法是使用for-loop

    var response = [  {    title: "Map marker 1",    lat: 10.0,    lng: 10.0  },  {    title: "Map marker 2",    lat: 11.0,    lng: 11.0  }],
        markers = [];
    
    for (var i = 0; i < response.length; i++) {
      markers[i]   = {position: { lat: response[i].lat, lng: response[i].lng }};
    }
    
    console.log(markers);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

      【解决方案3】:

      您可以通过使用所需属性的destructuring assignment 将Array#map 与值一起使用,然后将short hand properties 用作新对象。

      var response = [{ title: "Map marker 1", lat: 10.0, lng: 10.0 }, { title: "Map marker 2", lat: 11.0, lng: 11.0 }],
          array = response.map(({ lat, lng }) => ({ position: { lat, lng } }));
      
      console.log(array);
      .as-console-wrapper { max-height: 100% !important; top: 0; }

      【讨论】:

        【解决方案4】:
        response = [
          {
            title: "Map marker 1",
            lat: 10.0,
            lng: 10.0
          },
          {
            title: "Map marker 2",
            lat: 11.0,
            lng: 11.0
          }
        ];
        
        markers = [];
        
        for (var i in response) {
          markers.push({position: {lat: response[i].lat, lng: response[i].lng}});
        }
        
        // Some programmers hate the loop upstairs, so you can do like this and have exactly the same result:
        for (var i = 0; i < response.length; i++) {
          markers.push({position: {lat: response[i].lat, lng: response[i].lng}});
        }
        
        // Other programmers believe and I don't know why that map is better, so:
        markers = response.map(function (item) {
          return {position: {lat: item.lat, lng: item.lng}};
        });
        
        // The world is full of boring people, so we have programmers that think that only ECMA6 works, so:
        markers = response.map(item => {position: {lat: item.lat, lng: item.lng});
        
        // With any of those codes you will have this:
        markers = [
          {
            position: {lat: 10.0, lng: 10.0}
          }, 
          {
            position: {lat: 11.0, lng: 11.0}
          }
        ]
        

        【讨论】:

        • 我认为更好的方法是使用for-of-loop
        • 为什么有人会否决这个?这是完全有效的代码。是因为.map()不性感?这将是提供的任何答案中性能更高的代码! JSPerf 自己找出来。
        • 我调整为取悦大家...为什么人们这么无聊?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-07
        • 2012-11-14
        • 1970-01-01
        • 2014-11-19
        • 1970-01-01
        • 1970-01-01
        • 2015-12-13
        相关资源
        最近更新 更多