如何在一个大数组

时间:2017-05-02 07:19:28

标签: javascript lodash

想要合并两个对象数组。数组如下:

let vehicle_data = [
  {
    "id": 1,
    "make_text": "Peugeot",
    "model_text": "307",
    "color_text": "Bleu",
    "category_text": "CT",
    "vin": "654321",
    "autralis_id": 0
  }
]

let vehicle_slot_data = [
  {
    "vehicle_id": 1,
    "zone": "T",
    "side": "B",
    "col": 2,
    "handled": 0,
    "uploaded": 0
  },
  {
    "vehicle_id": 1,
    "zone": "A",
    "side": "E",
    "col": 1,
    "handled": 0,
    "uploaded": 0
  }
]

我想合并这两个。因此,我想要的结果是:

let result = [
    {
        "id": 1,
        "make_text": "Peugeot",
        "model_text": "307",
        "color_text": "Bleu",
        "category_text": "CT",
        "vin": "654321",
        "autralis_id": 0,
        "vehicle_id": 1,
        "zone": "T",
        "side": "B",
        "col": 2,
        "handled": 0,
        "uploaded": 0
    },
    {
        "id": 1,
        "make_text": "Peugeot",
        "model_text": "307",
        "color_text": "Bleu",
        "category_text": "CT",
        "vin": "654321",
        "autralis_id": 0,
        "vehicle_id": 1,
        "zone": "A",
        "side": "E",
        "col": 1,
        "handled": 0,
        "uploaded": 0
    }
]

我试着这样做:

let result = [];

vehicle_data.map(i => {
  vehicle_slot_data.map(j => {
    if (j.vehicle_id === i.id && j.handled === 0){
      result.push(Object.assign(i, j));
    }
  })
});

但是我得到了两个相同对象的结果:

let result = [
   {
      autralis_id: 0,
      category_text: "CT",
      col: 1,
      color_text: "Bleu",
      handled: 0,
      id: 1,
      make_text: "Peugeot",
      model_text: "307",
      side: "E",
      uploaded: 0,
      vehicle_id: 1,
      vin: "654321",
      zone: "A"
   },
   {
      autralis_id: 0,
      category_text: "CT",
      col: 1,
      color_text: "Bleu",
      handled: 0,
      id: 1,
      make_text: "Peugeot",
      model_text: "307",
      side: "E",
      uploaded: 0,
      vehicle_id: 1,
      vin: "654321",
      zone: "A"
   }
]

Here is the fiddle.

有什么建议吗?

5 个答案:

答案 0 :(得分:2)

嵌套循环似乎比你需要的更复杂。此外,如果您使用.map()返回值,那么您没有正确使用它,并且应该使用.forEach()进行简单迭代。

无论如何,也许是这样的:

let vehicle_data = [
  {
    "id": 1,
    "make_text": "Peugeot",
    "model_text": "307",
    "color_text": "Bleu",
    "category_text": "CT",
    "vin": "654321",
    "autralis_id": 0
  }
]

let vehicle_slot_data = [
  {
    "vehicle_id": 1,
    "zone": "T",
    "side": "B",
    "col": 2,
    "handled": 0,
    "uploaded": 0
  },
  {
    "vehicle_id": 1,
    "zone": "A",
    "side": "E",
    "col": 1,
    "handled": 0,
    "uploaded": 0
  }
]

let result = vehicle_slot_data
  .map(v => Object.assign({}, v, vehicle_data.find(m => m.id === v.vehicle_id)))

console.log(result)

请注意,您希望Object.assign()以新的空对象开头,如果没有匹配的元素,则.find()会返回undefined

编辑:我刚刚看到你的评论,如果两个数组中的项目不匹配(按ID),则不包括结果中的项目。扩展以下内容以获得相同的方法:

let vehicle_data = [
  {
    "id": 1,
    "make_text": "Peugeot",
    "model_text": "307",
    "color_text": "Bleu",
    "category_text": "CT",
    "vin": "654321",
    "autralis_id": 0
  }
]

let vehicle_slot_data = [
  {
    "vehicle_id": 1,
    "zone": "T",
    "side": "B",
    "col": 2,
    "handled": 0,
    "uploaded": 0
  },
  {
    "vehicle_id": 2,
    "zone": "A",
    "side": "E",
    "col": 1,
    "handled": 0,
    "uploaded": 0
  }
]

let result = []
vehicle_slot_data.forEach(v => {
  var match = vehicle_data.find(m => m.id === v.vehicle_id)
  if (match)
    result.push(Object.assign({}, v, match))
})
console.log(result)

答案 1 :(得分:1)

您需要使用空对象作为源对象。

result.push(Object.assign({}, i, j));
//                        ^^

为了获得更好的性能,我建议您使用Map并在地图中收集所有车辆数据,并使用Array#map作为广告位数据。



let vehicle_data = [{ id: 1, make_text: "Peugeot", model_text: "307", color_text: "Bleu", category_text: "CT", vin: "654321", autralis_id: 0 }],
    vehicle_slot_data = [{ vehicle_id: 1, zone: "T", side: "B", col: 2, handled: 0, uploaded: 0 }, { vehicle_id: 1, zone: "A", side: "E", col: 1, handled: 0, uploaded: 0 }],
    vehicleMap = new Map(vehicle_data.map(v => [v.id, v])),
    result = vehicle_slot_data.map(v => Object.assign({}, vehicleMap.get(v.vehicle_id), v));

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }




答案 2 :(得分:1)



let vehicle_data = [
  {
    "id": 1,
    "make_text": "Peugeot",
    "model_text": "307",
    "color_text": "Bleu",
    "category_text": "CT",
    "vin": "654321",
    "autralis_id": 0
  }
]

let vehicle_slot_data = [
  {
    "vehicle_id": 1,
    "zone": "T",
    "side": "B",
    "col": 2,
    "handled": 0,
    "uploaded": 0
  },
  {
    "vehicle_id": 1,
    "zone": "A",
    "side": "E",
    "col": 1,
    "handled": 0,
    "uploaded": 0
  }
]

let result = [];

vehicle_slot_data.map(i => {
      vehicle_data.map(j => {
        if (j.vehicle_id === i.id && i.handled === 0){
          result.push(Object.assign(i, j));
        }
      })
    });

console.log(result)

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.2.1/lodash.min.js"></script>
&#13;
&#13;
&#13;

只需交换地图:

  vehicle_slot_data.map(i => {
      vehicle_data.map(j => {
        if (j.vehicle_id === i.id && i.handled === 0){
          result.push(Object.assign(i, j));
        }
      })
    });

答案 3 :(得分:0)

你有没有尝试过&#34; concat&#34;?

&#13;
&#13;
function myFunction() {
    var hege = [{
    "id": 1,
    "make_text": "Peugeot",
    "model_text": "307",
    "color_text": "Bleu",
    "category_text": "CT",
    "vin": "654321",
    "autralis_id": 0
  }];
    var stale = [{
    "vehicle_id": 1,
    "zone": "T",
    "side": "B",
    "col": 2,
    "handled": 0,
    "uploaded": 0
  },
  {
    "vehicle_id": 1,
    "zone": "A",
    "side": "E",
    "col": 1,
    "handled": 0,
    "uploaded": 0
  }];
    var children = hege.concat(stale); 
    document.getElementById("demo").innerHTML = JSON.stringify(children);
}
&#13;
<!DOCTYPE html>
<html>
<body>

<p>Click the button to join two arrays.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

</body>
</html>
&#13;
&#13;
&#13;

我希望它可以帮到你!

答案 4 :(得分:0)

您似乎希望将vehicle_slot_data[0]vehicle_data[0]vehicle_slot_data[1]vehicle_data[0]合并,因此我建议使用jquery extend方法,或者您可以编写功能做同样的工作

var result=[
            $.extend({},vehicle_slot_data[0],vehicle_data[0]),
            $.extend({},vehicle_slot_data[1],vehicle_data[0])
           ]