0

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?

enter image description here

I have a table with 3 rows shown above(SquadDetails)

  1. 1st row 1st column Molecule Man/Radiation resistance
  2. 1st-row 2nd column Molecule Man/Radiation blast
  3. 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

  1. 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

  1. 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"}
  1. 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"}
7
  • @MisterJojo memberswithpower[x].id is unique . name can not be a unique value. the same name can be used in different squadName. Commented Oct 3, 2022 at 11:27
  • power details example:[ { "id":1, "name": "Molecule Man", "powers": "Radiation resistance" }, { "id":1, "name": "Molecule Man", "powers":"Radiation blast" }, { "id":2, "name": "Eternal Flame", "powers":"Turning tiny" } ] Commented Oct 3, 2022 at 12:00
  • NO it's not same .we can have different scnerio like this [ { "id":1, "name": "Molecule Man", "powers": "Radiation resistance" }, { "id":1, "name": "Molecule Man", "powers":"Turning tiny" }, { "id":1, "name": "ABC", "powers":"Turning tiny" } ] Commented Oct 3, 2022 at 13:22
  • In this case I do not believe that your problem is solvable, because the logic of your data is inconsistent. Commented Oct 3, 2022 at 13:27
  • @MisterJojo how to resolve this ? Commented Oct 3, 2022 at 16:06

1 Answer 1

3
+25

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);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.