its me
its me

Reputation: 542

How to match two different array set and update non matched object in javascript?

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"}

Upvotes: 0

Views: 227

Answers (1)

Roh&#236;t J&#237;ndal
Roh&#236;t J&#237;ndal

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

Related Questions