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)
- 1st row 1st column Molecule Man/Radiation resistance
- 1st-row 2nd column Molecule Man/Radiation blast
- 2nd row 1st column Eternal Flame/Turning tiny
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
- 1st-row 3rd column Molecule Man/Turning tiny
Explanation: In 1st row {"id":1, "name": "Molecule Man", "powers":"Turning tiny"} is not matching so we have to add this in 1st row
- 2nd row 2nd column Eternal Flame/Radiation resistance
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"}
- 3rd row 1st column Madame Uppercut/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"}
