从一个数组中提取项目并添加到现有数组中 [英] pull items from one array and add to an existing array Javascript

查看:90
本文介绍了从一个数组中提取项目并添加到现有数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已有阵列,想向ID匹配的每个项目添加另一个对象阵列.

I have existing array and would like to add another object array to each item where the IDs match.

var existingData = [
    {
        "id": "0100",
        "name": "name 1",
        "message": [
            "Lorem blah blah 1",
            "Lorem blah blah 1.1"
        ]
    },
    {
        "id": "0200",
        "name": "name 2",
        "message": [
            "Lorem blah blah 2",
            "Lorem blah blah 2.1",
            "Lorem blah blah 2.2"
        ]
    },
    {
        "id": "0300",
        "name": "name 3",
        "message": [
            "Lorem blah blah 3",
            "Lorem blah blah 3.1",
            "Lorem blah blah 3.2",
            "Lorem blah blah 3.3",
            "Lorem blah blah 3.4"
        ]
    }
];

下面的数组是我的第二个数组,其中的"relatedId"将与existingArray中的ID相同:

and the following array is my second array in which the "relatedId" will be the same as the IDs in the existingArray:

var data2 = [
  {"CandidateName": "Mary", "relatedId": ["0100", "0200"]},
  { "CandidateName": "John", "relatedId": ["0200"]},
  { "CandidateName":"Peter", "relatedId": ["0300", "0100"]},
  { "CandidateName": "Paul", "relatedId": ["0300"]}
];

如果ID匹配,我想从data2中提取"CandidateName"并将其放入现存的数据中.

If IDs match, I want to pull the "CandidateName" from data2 and put it into the existingData.

此特定问题已从此处扩展=> Group Javascript array of ID的对象

This particular question is has been extended from here => Group Javascript array of objects by ID

我已经尝试过了,但是由于浏览器挂起,我走得还很远:

I have already made an attempt but I am not getting very far as the browser hangs:

var result = data.reduce(function(r, el) {

  // THIS INNER LOOP MAKES THE BROWSER HANG!!!
  data2.forEach(function (a){
    console.log('a',a); 
  });

  var e = el.id.slice(0, 2);
  if (!o[e]) {
    o[e] = {
      id: el.id,
      name: el.name,
      message: []
    }
    r.push(o[e]);
  }
  o[e].message.push(el.message);
  return r;
}, [])

所以我想结束这样的事情:

So I want to end up with something like this:

var existingData = [
    {
        "id": "0100",
        "name": "name 1",
        "message": [
            "Lorem blah blah 1",
            "Lorem blah blah 1.1"
        ],
        "CandidateName": ["Mary", "Peter"]
    },
    {
        "id": "0200",
        "name": "name 2",
        "message": [
            "Lorem blah blah 2",
            "Lorem blah blah 2.1",
            "Lorem blah blah 2.2"
        ],
        "CandidateName": ["Mary", "John"]
    },
    {
        "id": "0300",
        "name": "name 3",
        "message": [
            "Lorem blah blah 3",
            "Lorem blah blah 3.1",
            "Lorem blah blah 3.2",
            "Lorem blah blah 3.3",
            "Lorem blah blah 3.4"
        ],
        "CandidateName": ["Peter", "Paul"]
    }
]

推荐答案

这是另一种ES6解决方案:

Here is another ES6 solution:

data2.forEach( function (obj) {
    obj.relatedId.forEach( id => this.get(id).candidateName.push(obj.CandidateName));
}, new Map(existingData.map (
    obj => [obj.id, Object.assign(obj, { 'candidateName': [] } )] 
)));

var existingData = [
    {
        "id": "0100",
        "name": "name 1",
        "message": [
            "Lorem blah blah 1",
            "Lorem blah blah 1.1"
        ]
    },
    {
        "id": "0200",
        "name": "name 2",
        "message": [
            "Lorem blah blah 2",
            "Lorem blah blah 2.1",
            "Lorem blah blah 2.2"
        ]
    },
    {
        "id": "0300",
        "name": "name 3",
        "message": [
            "Lorem blah blah 3",
            "Lorem blah blah 3.1",
            "Lorem blah blah 3.2",
            "Lorem blah blah 3.3",
            "Lorem blah blah 3.4"
        ]
    }
];

var data2 = [
  {"CandidateName": "Mary", "relatedId": ["0100", "0200"]},
  { "CandidateName": "John", "relatedId": ["0200"]},
  { "CandidateName":"Peter", "relatedId": ["0300", "0100"]},
  { "CandidateName": "Paul", "relatedId": ["0300"]}
];

data2.forEach( function (obj) {
    obj.relatedId.forEach( id => this.get(id).candidateName.push(obj.CandidateName));
}, new Map(existingData.map (
    obj => [obj.id, Object.assign(obj, { 'candidateName': [] } )] 
)));

console.log(existingData);

这会将 existingData 转换为地图,以 id 为键,同时将 candidateName 数组(空)添加到对象.

This turns the existingData into a map, keyed by id, while adding the candidateName array (empty) to the objects.

此映射作为第二个参数传递给forEach,因此将其定义为this对象.

This map is passed as the second argument to forEach, thus defining it as the this object.

data2 元素上的forEach内,迭代 relatedId 值,对于每个 id 值,相应的存在数据对象的 candidateName 数组用 CandidateName 扩展.

Inside the forEach on data2 elements, the relatedId values are iterated and for each of these id values the corresponding existingData object's candidateName array is extended with the CandidateName.

这篇关于从一个数组中提取项目并添加到现有数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆