1
array1 = [{
    questionId: "5e52b55330bb2cee102b9a39",
    note: "Name",
    prefillValue: "prasanna"
},

{
    questionId: "5e52b56b30bb2cee102b9a3f",
    note: "Mobile Number",
    prefillValue: null
},
{
    questionId: "5e52b58230bb2cee102b9a42",
    note: "Agent Email",
    prefillValue: null
},
{
    questionId: "5e52b55e30bb2cee102b9a3c",
    note: "Email",
    prefillValue: null
},
{
    questionId: "5e52c39730bb2cee102b9a47",
    note: "Agent ID",
    prefillValue: null
},
{
    questionId: "5e54dbdd30bb2c6018f488ca",
    note: "Location",
    prefillValue: null
}
]


array2 = [{
    questionId: "5e52b55e30bb2cee102b9a3c",
    note: "Email",
    prefillValue: "[email protected]"
},
{
    questionId: "5e52c39730bb2cee102b9a47",
    note: "Agent ID",
    prefillValue: "34"
},
{
    questionId: "5e54dbdd30bb2c6018f488ca",
    note: "Location",
    prefillValue: "Chennai"
}
]

Expected output:

array3 = [{
        questionId: "5e52b55330bb2cee102b9a39",
        note: "Name",
        prefillValue: "prasanna"
    },

    {
        questionId: "5e52b56b30bb2cee102b9a3f",
        note: "Mobile Number",
        prefillValue: null
    }, {
        questionId: "5e52b58230bb2cee102b9a42",
        note: "Agent Email",
        prefillValue: null
    }, {
        questionId: "5e52b55e30bb2cee102b9a3c",
        note: "Email",
        prefillValue: null
    }, {
        questionId: "5e52b55e30bb2cee102b9a3c",
        note: "Email",
        prefillValue: "[email protected]"
    }, {
        questionId: "5e52c39730bb2cee102b9a47",
        note: "Agent ID",
        prefillValue: "34"
    },
    {
        questionId: "5e54dbdd30bb2c6018f488ca",
        note: "Location",
        prefillValue: "Chennai"
    }
]
1
  • 1
    Are you asking how to merge arrays without ending up with duplicates? Commented Mar 19, 2020 at 17:53

4 Answers 4

4

let array1 = [{
    questionId: "5e52b55330bb2cee102b9a39",
    note: "Name",
    prefillValue: "prasanna"
},

{
    questionId: "5e52b56b30bb2cee102b9a3f",
    note: "Mobile Number",
    prefillValue: null
},
{
    questionId: "5e52b58230bb2cee102b9a42",
    note: "Agent Email",
    prefillValue: null
},
{
    questionId: "5e52b55e30bb2cee102b9a3c",
    note: "Email",
    prefillValue: null
},
{
    questionId: "5e52c39730bb2cee102b9a47",
    note: "Agent ID",
    prefillValue: null
},
{
    questionId: "5e54dbdd30bb2c6018f488ca",
    note: "Location",
    prefillValue: null
}
]


let array2 = [{
    questionId: "5e52b55e30bb2cee102b9a3c",
    note: "Email",
    prefillValue: "[email protected]"
},
{
    questionId: "5e52c39730bb2cee102b9a47",
    note: "Agent ID",
    prefillValue: "34"
},
{
    questionId: "5e54dbdd30bb2c6018f488ca",
    note: "Location",
    prefillValue: "Chennai"
}
]

let array3 = [...array1,...array2];

console.log(array3)

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

8 Comments

This is not an array of objects as the OP described.
it should work with the object provided in the question,
I need to remove duplicated QuestionID from the first array
Then you should filter them before merging
@Prasannawatson take a look at here
|
1

You can use Object.assign to copy properties from one object to another

const array1 = [{
      questionId: "5e52b55330bb2cee102b9a39",
      note: "Name",
      prefillValue: "prasanna"
  },

  {
      questionId: "5e52b56b30bb2cee102b9a3f",
      note: "Mobile Number",
      prefillValue: null
  },
  {
      questionId: "5e52b58230bb2cee102b9a42",
      note: "Agent Email",
      prefillValue: null
  },
  {
      questionId: "5e52b55e30bb2cee102b9a3c",
      note: "Email",
      prefillValue: null
  },
  {
      questionId: "5e52c39730bb2cee102b9a47",
      note: "Agent ID",
      prefillValue: null
  },
  {
      questionId: "5e54dbdd30bb2c6018f488ca",
      note: "Location",
      prefillValue: null
  }
]


const array2 = [{
      questionId: "5e52b55e30bb2cee102b9a3c",
      note: "Email",
      prefillValue: "[email protected]"
  },
  {
      questionId: "5e52c39730bb2cee102b9a47",
      note: "Agent ID",
      prefillValue: "34"
  },
  {
      questionId: "5e54dbdd30bb2c6018f488ca",
      note: "Location",
      prefillValue: "Chennai"
  }
]


const array3 = array1.map(o => Object.assign(o, array2.find(a => a.questionId === o.questionId)));

console.log(array3)

Comments

0

let array1 = [{
    questionId: "5e52b55330bb2cee102b9a39",
    note: "Name",
    prefillValue: "prasanna"
},

{
    questionId: "5e52b56b30bb2cee102b9a3f",
    note: "Mobile Number",
    prefillValue: null
},
{
    questionId: "5e52b58230bb2cee102b9a42",
    note: "Agent Email",
    prefillValue: null
},
{
    questionId: "5e52b55e30bb2cee102b9a3c",
    note: "Email",
    prefillValue: null
},
{
    questionId: "5e52c39730bb2cee102b9a47",
    note: "Agent ID",
    prefillValue: null
},
{
    questionId: "5e54dbdd30bb2c6018f488ca",
    note: "Location",
    prefillValue: null
}
]


let array2 = [{
    questionId: "5e52b55e30bb2cee102b9a3c",
    note: "Email",
    prefillValue: "[email protected]"
},
{
    questionId: "5e52c39730bb2cee102b9a47",
    note: "Agent ID",
    prefillValue: "34"
},
{
    questionId: "5e54dbdd30bb2c6018f488ca",
    note: "Location",
    prefillValue: "Chennai"
}
]


let array3 = array1.concat(...array2);

console.log(array3)

Comments

0

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

You can remove duplicate keys in this way.

let array1 = [{
    questionId: "5e52b55330bb2cee102b9a39",
    note: "Name",
    prefillValue: "prasanna"
},

{
    questionId: "5e52b56b30bb2cee102b9a3f",
    note: "Mobile Number",
    prefillValue: null
},
{
    questionId: "5e52b58230bb2cee102b9a42",
    note: "Agent Email",
    prefillValue: null
},
{
    questionId: "5e52b55e30bb2cee102b9a3c",
    note: "Email",
    prefillValue: null
},
{
    questionId: "5e52c39730bb2cee102b9a47",
    note: "Agent ID",
    prefillValue: null
},
{
    questionId: "5e54dbdd30bb2c6018f488ca",
    note: "Location",
    prefillValue: null
}
]

var uniq = {}
var arrFiltered = arrays1.filter(obj => !uniq[obj.questionId] && (uniq[obj.questionId] = true));
console.log('Filtered Array:', arrFiltered)

reference can be found here

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.