[英]how to merge two array of object in following format using javascript
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: "skk@gmail.com"
},
{
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: "skk@gmail.com"
}, {
questionId: "5e52c39730bb2cee102b9a47",
note: "Agent ID",
prefillValue: "34"
},
{
questionId: "5e54dbdd30bb2c6018f488ca",
note: "Location",
prefillValue: "Chennai"
}
]
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: "skk@gmail.com" }, { questionId: "5e52c39730bb2cee102b9a47", note: "Agent ID", prefillValue: "34" }, { questionId: "5e54dbdd30bb2c6018f488ca", note: "Location", prefillValue: "Chennai" } ] let array3 = [...array1,...array2]; console.log(array3)
You can use Object.assign
to copy properties from one object to another您可以使用
Object.assign
将属性从一个对象复制到另一个对象
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: "skk@gmail.com" }, { 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)
The filter() method creates a new array with all elements that pass the test implemented by the provided function. filter() 方法创建一个新数组,其中包含通过所提供函数实现的测试的所有元素。
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)
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: "skk@gmail.com" }, { questionId: "5e52c39730bb2cee102b9a47", note: "Agent ID", prefillValue: "34" }, { questionId: "5e54dbdd30bb2c6018f488ca", note: "Location", prefillValue: "Chennai" } ] let array3 = array1.concat(...array2); console.log(array3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.