1

I have two Arrays of Objects:

{
"records": [
  {
      "lead_id": 97195,
      "user_id": 613,
      "segment_id": 0,
      "email_address": "[email protected]",
      "full_name": "asdf asdf",
  },
  {
      "lead_id": 97197,
      "user_id": 613,
      "segment_id": 0,
      "email_address": "[email protected]",
      "full_name": "asdf asdf",
  }
  ]  
}

And the second array of objects:

  {
       "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"
        }
      ]
    }

I want to insert the tagname on specific lead_id object, for example in records there's "lead_id" : 97195 and the userTags "lead_id" : 97195, that's my catcher or hint that should be connected on that Array of objects but I dont have any idea how can I insert the tag_name object in the records list

example output

"records": [
  {
      "lead_id": 97195,
      "user_id": 613,
      "segment_id": 0,
      "email_address": "[email protected]",
      "full_name": "asdf asdf",.
      "tag_name": "Julien",
  }]
2
  • This is the only expected output ? you have 2 lead_id records and 4 lead_id in userTags Commented Feb 6, 2019 at 11:43
  • @GovindParashar Hi actually all that have same lead_id will be join in the records array, I just make it short, sorry man if make it more confusing Commented Feb 7, 2019 at 0:30

2 Answers 2

1

You can do this using map and find to iterate over your records and add the tag_name field that you find in the userTags object.

However, you seem to have multiple tag_name for the same lead_id. I have added two example, one that takes the tag_name of the first matching lead_id and one that collects all tag_name for all matching lead_id:

const records = {
  "records": [
    { "lead_id": 97195, "user_id": 613, "segment_id": 0, "email_address": "[email protected]", "full_name": "asdf asdf" },
    { "lead_id": 97197, "user_id": 613, "segment_id": 0, "email_address": "[email protected]", "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);

Sign up to request clarification or add additional context in comments.

2 Comments

Hi mate thanks for the showing me the solution,I have a question for return { ...record, tag_name: tag.tag_name }; hows that execute ?
@Mike Victoria, { ...record } will spread the content of record in the new object, it is called the spread operator: see the doc here
0

This is my way… maybe rough but it works :)

let main_array = {
    "records": [
        {
            "lead_id": 97195,
            "user_id": 613,
            "segment_id": 0,
            "email_address": "[email protected]",
            "full_name": "asdf asdf",
        },
        {
            "lead_id": 97197,
            "user_id": 613,
            "segment_id": 0,
            "email_address": "[email protected]",
            "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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.