Input :
// First Array
const input1 = [{
'name': "name1",
'email': "[email protected]",
'age': 10,
'score':95,
'address': {
'city': "city1"
}
},
{
'name': "name2",
'email': "[email protected]",
'age': 10,
'score':45,
'address': {
'city': "city2"
}
}
];
// Second Array
const input2 = [{
'id': 1,
'fullname': "name1",
'emailaddress': "[email protected]",
'age': 10,
'score':45,
'address': {
'city': "city1"
}
},
{
'id': 5,
'name': "name2",
'email': "[email protected]",
'age': 20,
'score':55,
'address': {
'city': "city2"
}
}
];
const filter1 = [{
"filter1Key": "age",
"filter2Key": "score"
}];
const filter2 = [{
"filter1Key": "name",
"filter2Key": "address.city"
}];
const newArray = [];
cont updateArray = [];
//Below code is not is giving
const test1 = input1.filter((data) => input2.some((obj) =>
filter1.every(key => data[key.filter1Key] === obj[key.filter2Key])?filter2.every(key => data[key.filter1Key] === obj[key.filter2Key])?'':updateArray.push(obj):newArray.push(obj)));
console.log(test1);
First all the unmatched record with filter1 of input1 should be push into newArray and unmatched record with filter2 of input1 should be push into updateArray but id of inout2 should also push with that record
Expected output:
newArray = [{
'name': "name1",
'email': "[email protected]",
'age': 10,
'score':95,
'address': {
'city': "city1"
}
}];
updateArray = [{
'id': 5,
'name': "name2",
'email': "[email protected]",
'age': 10,
'score':45,
'address': {
'city': "city2"
}
}]
CodePudding user response:
Convert the first ? to && and the second ? to == and the first : to ?
CodePudding user response:
you may change below code by your es6 syntax-- I wish It might help you
input1.forEach((input1Item) => {
const x = input2.find((input2Item) =>{
return input1Item[filter1[0].filter1Key] === input2Item[filter1[0].filter1Key] && input1Item[filter1[0].filter2Key] === input2Item[filter1[0].filter2Key]
})
const y = input2.find((input2Item) =>{
return input1Item[filter1[0].filter1Key] !== input2Item[filter1[0].filter1Key] || input1Item[filter1[0].filter2Key] !== input2Item[filter1[0].filter2Key]
})
if(!x){
newArray.push(input1Item)
} else if(y){
updateArray.push({ id: y.id , ...input1Item})
}
});