我有两个对象数组。 我想做的是:
我要比较的两个对象:
data_1 = [
{name: "Mike", age: 30},
{name: "Kelly", age: 25},
{id: "A1", name: "Jack", age: 20}
]
data_2 = [
{id: "A2", name: "Mike"},
{id: "A3", name: "Kelly"}
]
具有完整数据的新对象:
data_3 = [
{id: "A1", name: "Jack", age: 20}
{id: "A2", name: "Mike", age: 30},
{id: "A3", name: "Kelly", age: 25}}
]
我尝试使用Array.prototype.forEach()
,但效果不佳...:(
答案 0 :(得分:1)
let data_1 = [{name: "Mike", age: 30},{name: "Kelly", age: 25},{id: "A1", name: "Jack", age: 20}]
let data_2 = [{id: "A2", name: "Mike"},{id: "A3", name: "Kelly"}]
let merged = [...data_1,...data_2].reduce((op,{name,id,age}) => {
if( op[name] ){
op[name].id = id != undefined ? id : op[name].id
} else {
op[name] = {
name,
age,
id: id!=undefined ? id : 'Not available'
}
}
return op
},{})
console.log(Object.values(merged))
答案 1 :(得分:1)
您可以像这样使用map
,some
和find
,并进行结构分解以使其变得简单:
const data_1 = [{
name: "Mike",
age: 30
},
{
name: "Kelly",
age: 25
},
{
id: "A1",
name: "Jack",
age: 20
}
];
const data_2 = [{
id: "A2",
name: "Mike"
},
{
id: "A3",
name: "Kelly"
}
];
function combineArrays(arr1, arr2) {
let result = arr1.map(obj => {
if (obj.name && obj.age && obj.id) return obj;
if (arr2.some(({ id, name }) => obj.name == name)) {
let { id, name } = arr2.find(e => obj.name == e.name);
return {
id,
name,
age: obj.age
};
}
});
return result.sort((a, b) => +a.id[1] - +b.id[1]);
}
const data_3 = combineArrays(data_1, data_2);
console.log(data_3);
答案 2 :(得分:1)
如果您想要更少的代码。您可以使用lodash
data_1 = [{name: "Mike", age: 30},{name: "Kelly", age: 25},{id: "A1", name: "Jack", age: 20}]
data_2 = [{id: "A2", name: "Mike"},{id: "A3", name: "Kelly"}]
let d1 = _.keyBy(data_1, 'name')
let d2 = _.keyBy(data_2, 'name')
let rs = _.merge(d1, d2)
console.log(_.values(rs))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
答案 3 :(得分:0)
data_1 = [{
name: "Mike",
age: 30
},
{
id:"A4",
name: "Simon",
age: 25
},
{
id: "A1",
name: "Jack",
age: 20
}
]
data_2 = [{
id: "A2",
name: "Mike"
},
{
id: "A3",
name: "Kelly"
}
]
data_3 = [];
for (let i = 0, imax = data_1.length; i < imax; i++) {
if (typeof data_2[i] != "undefined") { // this will check undefined index of different range
if (typeof data_2[i].name != "undefined") { // this will check name attribute assume some times only get id and age prob if not remove this
if (data_1[i].name == data_2[i].name) { // this will compare 2 name if same merge it into 1 object
let obj = extend(data_2[i], data_1[i]);
data_3.push(obj) // then push to the array merged object
}else{ // this else will push if name does not match but some id name bla bla..
data_3.push(data_1[i]);
}
}
} else { // this else will push if data_1 array longer than data_2 indexes values goes in here
data_3.push(data_1[i]);
}
}
// this function extend 2 object got
function extend(obj, src) {
for (var key in src) {
if (src.hasOwnProperty(key)) obj[key] = src[key];
}
return obj;
}
console.log(data_3)