Reputation: 542
I have two different arrays of the object(SquadDetails,powerDetails).
I have to match the following condition
SquadDetails.memberswithpower.id == powerDetails.id and SquadDetails.memberswithpower.powers = powerDetails.powers/ SquadDetails.memberswithpower.name= powerDetails.name
How can match id and powers/name? if not matched add that object into powerDetails.
could someone advise on this?
var SquadDetails = [{
"squadName": "Super hero squad",
"homeTown": "Metro City",
"formed": 2016,
"memberswithpower": [
{
"id":1,
"name": "Molecule Man",
"powers": "Radiation resistance"
},
{
"id":1,
"name": "Molecule Man",
"powers":"Turning tiny"
}
]
},
{
"squadName": "ABC squad",
"homeTown": "ABC",
"formed": 2017,
"memberswithpower": [
{
"id":2,
"name": "Eternal Flame",
"powers": "Radiation resistance"
}
]
},
{
"squadName": "XYZ squad",
"homeTown": "XYZ",
"formed": 2017,
"memberswithpower": [
{
"id":3,
"name": "Madame Uppercut",
"powers": "Radiation resistance"
}
]
},
{
"squadName": "wsx squad",
"homeTown": "XYZ",
"formed": 2018,
"memberswithpower": []
}
];
var powerDetails = [
{
"id":1,
"name": "Molecule Man",
"powers": "Radiation resistance"
},
{
"id":1,
"name": "Molecule Man",
"powers":"Radiation blast"
},
{
"id":2,
"name": "Eternal Flame",
"powers":"Turning tiny"
}
]
console.log(SquadDetails);
var filter =
SquadDetails.filter(SD =>
<!-- SD.memberswithpower.filter(MWP => -->
<!-- console.log(MWP.id) -->
<!-- <!-- powerDetails.filter(PD => --> -->
<!-- <!-- PD.id == MWP.id && PD.powers == MWP.powers --> -->
<!-- <!-- ) --> -->
<!-- ) -->
SD.some(function (arrVal) {
console.log(arrVal)
});
)
Expected output:
[
{
"id":1,
"name": "Molecule Man",
"powers": "Radiation resistance"
},
{
"id":1,
"name": "Molecule Man",
"powers":"Turning tiny"
},
{
"id":1,
"name": "Molecule Man",
"powers":"Radiation blast"
}
{
"id":2,
"name": "Eternal Flame",
"powers": "Radiation resistance"
}
{
"id":2,
"name": "Eternal Flame",
"powers":"Turning tiny"
},
{
"id":3,
"name": "Madame Uppercut",
"powers": "Radiation resistance"
}
]
I have tried filter and some methods but getting errors. could someone help me with this?
I have a table with 3 rows shown above(SquadDetails)
Now I have to compare Powerdetails
with SquadDetails
and I have to update non matched row in the Powerdetails which means(based on provided data) I have to add
Explanation: In 1st row {"id":1, "name": "Molecule Man", "powers":"Turning tiny"}
is not matching so we have to add this in 1st row
Explanation: In the 2nd row below item is not matching so we have to add this in 2nd row
{"id":2,"name": "Eternal Flame","powers":"Radiation resistance"}
Explanation: In the 3rd row below item is not present so we have to add this in 3rd row
{"id":3,"name": "Madame Uppercu","powers":"Radiation resistance"}
Upvotes: 0
Views: 227
Reputation: 27232
You can simply achieve this with a minimal development effort by comparing the JSON strings (Convert the objects into string) by using JSON.stringify
and then check the index in the of the JSON string into powerDetails
string.
Live Demo :
var SquadDetails = [{
"squadName": "Super hero squad",
"homeTown": "Metro City",
"formed": 2016,
"memberswithpower": [
{
"id":1,
"name": "Molecule Man",
"powers": "Radiation resistance"
},
{
"id":1,
"name": "Molecule Man",
"powers":"Turning tiny"
}
]
}, {
"squadName": "ABC squad",
"homeTown": "ABC",
"formed": 2017,
"memberswithpower": [
{
"id":2,
"name": "Eternal Flame",
"powers": "Radiation resistance"
}
]
}, {
"squadName": "XYZ squad",
"homeTown": "XYZ",
"formed": 2017,
"memberswithpower": [
{
"id":3,
"name": "Madame Uppercut",
"powers": "Radiation resistance"
}
]
}, {
"squadName": "wsx squad",
"homeTown": "XYZ",
"formed": 2018,
"memberswithpower": []
}];
var powerDetails = [{
"id":1,
"name": "Molecule Man",
"powers": "Radiation resistance"
}, {
"id":1,
"name": "Molecule Man",
"powers":"Radiation blast"
}, {
"id":2,
"name": "Eternal Flame",
"powers":"Turning tiny"
}];
SquadDetails.forEach(({ memberswithpower }) => {
memberswithpower.forEach(obj => {
if (JSON.stringify(powerDetails).indexOf(JSON.stringify(obj)) === -1) {
powerDetails.push(obj);
}
})
});
console.log(powerDetails);
Upvotes: 3