在对象JavaScript数组中插入对象

时间:2019-02-06 11:01:17

标签: javascript lodash

我有两个对象数组:

{
"records": [
  {
      "lead_id": 97195,
      "user_id": 613,
      "segment_id": 0,
      "email_address": "asdf@lkasd.asd",
      "full_name": "asdf asdf",
  },
  {
      "lead_id": 97197,
      "user_id": 613,
      "segment_id": 0,
      "email_address": "asdfasdfasdf@asdasdasd.com",
      "full_name": "asdf asdf",
  }
  ]  
}

第二个对象数组:

  {
       "userTags": [
        {
          "lead_id": 97195,
          "user_tag_id": 93,
          "tag_name": "Julien",
        },
        {
          "lead_id": 97195,
          "user_tag_id": 93,
          "tag_name": "test123"
        },
        {
          "lead_id": 97197,
          "user_tag_id": 93,
          "tag_name": "General Business"
        },
        {
          "lead_id": 97197,
          "user_tag_id": 93,
          "tag_name": "PAREB CLRB"
        }
      ]
    }

我想在特定的lead_id对象上插入标记名,例如在记录"lead_id" : 97195和userTags "lead_id" : 97195中,这就是我的捕获器或提示,应该在该数组上连接对象,但我不知道如何在tag_name列表中插入records对象

示例输出

"records": [
  {
      "lead_id": 97195,
      "user_id": 613,
      "segment_id": 0,
      "email_address": "asdf@lkasd.asd",
      "full_name": "asdf asdf",.
      "tag_name": "Julien",
  }]

2 个答案:

答案 0 :(得分:1)

您可以使用mapfind遍历记录,并添加在tag_name对象中找到的userTags字段。

但是,对于同一tag_name,您似乎有多个lead_id。 我添加了两个示例,一个示例采用第一个匹配的tag_name中的lead_id,另一个示例为所有匹配的tag_name收集所有lead_id

const records = {
  "records": [
    { "lead_id": 97195, "user_id": 613, "segment_id": 0, "email_address": "asdf@lkasd.asd", "full_name": "asdf asdf" },
    { "lead_id": 97197, "user_id": 613, "segment_id": 0, "email_address": "asdfasdfasdf@asdasdasd.com", "full_name": "asdf asdf" }
  ]  
};

const userTags = {
  "userTags": [
    { "lead_id": 97195, "user_tag_id": 93, "tag_name": "Julien" },
    { "lead_id": 97195, "user_tag_id": 93, "tag_name": "test123" },
    { "lead_id": 97197, "user_tag_id": 93, "tag_name": "General Business" },
    { "lead_id": 97197, "user_tag_id": 93, "tag_name": "PAREB CLRB" }
  ]
};

const newRecords = {
  records: records.records.map(record => {
    const lead_id = record.lead_id;
    const tag = userTags.userTags.find(tag => tag.lead_id === lead_id);
    return { ...record, tag_name: tag.tag_name };
  })
};

const newRecordsMult = {
  records: records.records.map(record => {
    const lead_id = record.lead_id;
    const tags = userTags.userTags
      .filter(tag => tag.lead_id === lead_id)
      .map(tag => tag.tag_name);
    return { ...record, tag_name: tags };
  })
}

console.log(newRecords, newRecordsMult);

答案 1 :(得分:0)

这是我的方式……也许很粗略,但效果不错:)

let main_array = {
    "records": [
        {
            "lead_id": 97195,
            "user_id": 613,
            "segment_id": 0,
            "email_address": "asdf@lkasd.asd",
            "full_name": "asdf asdf",
        },
        {
            "lead_id": 97197,
            "user_id": 613,
            "segment_id": 0,
            "email_address": "asdfasdfasdf@asdasdasd.com",
            "full_name": "asdf asdf",
        }
    ]
}

let tag_array = {
    "userTags": [
        {
            "lead_id": 97195,
            "user_tag_id": 93,
            "tag_name": "Julien",
        },
        {
            "lead_id": 97195,
            "user_tag_id": 93,
            "tag_name": "test123"
        },
        {
            "lead_id": 97197,
            "user_tag_id": 93,
            "tag_name": "General Business"
        },
        {
            "lead_id": 97197,
            "user_tag_id": 93,
            "tag_name": "PAREB CLRB"
        }
    ]
}

//loop main array
main_array["records"].forEach(function(item){
    let lead_id = parseInt(item.lead_id);
    let temp_array = [];

    //loop tags
    tag_array["userTags"].forEach(function(tag){
        if(lead_id === parseInt(tag.lead_id)){
            temp_array.push(tag.tag_name);
        }
    });

    item.tags = temp_array;
});

console.log(main_array);