简体   繁体   中英

How to compare and remove the object from array if value is same in Javascript?

Here i am having two objects

let obj1 = {
  "name": "Gym",
  "createdAt": "2021/01/14 04:17:01",
  "id": "1f137fd1-0d3c-4eb0-b045-a13a77971887",
  "songs": [
    {
      "userId": 1,
      "id": 1,
      "title": "quidem molestiae enim",
      "songId": "36a8d4a7-eeb1-4f1a-81d4-20f3ce682da9"
    },
    {
      "userId": 1,
      "id": 1,
      "title": "quidem molestiae enim",
      "songId": "e0009b28-cfe4-455f-ada4-21d2b79c9ee9"
    }
  ]
}

let obj2 = {userId: 1, id: 1, title: "quidem molestiae enim", songId: "36a8d4a7-eeb1-4f1a-81d4-20f3ce682da9"}

From the above 2 objects I need to loop the obj1 array and compare its songs.songId === obj2.songId. If value are same I need to cut that particular object from the songs array.

Example ,

songId in obj1.songs[0].songId === obj2.songId . Because the value is same, have to cut the song object form the songs array. The result has to be like this.

o/p

{
  "name": "Gym",
  "createdAt": "2021/01/14 04:17:01",
  "id": "1f137fd1-0d3c-4eb0-b045-a13a77971887",
  "songs": [
    {
      "userId": 1,
      "id": 1,
      "title": "quidem molestiae enim",
      "songId": "e0009b28-cfe4-455f-ada4-21d2b79c9ee9"
    }
  ]
}

Your suggestion or help is much appreciated. Thanks in advance

You could use Array.prototype.filter() method.

Filter method returns a new array with all array items that pass the provided function which test each item of the array.

 let obj1 = { name: 'Gym', createdAt: '2021/01/14 04:17:01', id: '1f137fd1-0d3c-4eb0-b045-a13a77971887', songs: [ { userId: 1, id: 1, title: 'quidem molestiae enim', songId: '36a8d4a7-eeb1-4f1a-81d4-20f3ce682da9', }, { userId: 1, id: 1, title: 'quidem molestiae enim', songId: 'e0009b28-cfe4-455f-ada4-21d2b79c9ee9', }, ], }; const obj2 = { userId: 1, id: 1, title: 'quidem molestiae enim', songId: '36a8d4a7-eeb1-4f1a-81d4-20f3ce682da9', }; obj1.songs = obj1.songs.filter((x) => x.songId.== obj2;songId). console;log(obj1);

 var obj1 = { "name": "Gym", "createdAt": "2021/01/14 04:17:01", "id": "1f137fd1-0d3c-4eb0-b045-a13a77971887", "songs": [ { "userId": 1, "id": 1, "title": "quidem molestiae enim", "songId": "36a8d4a7-eeb1-4f1a-81d4-20f3ce682da9" }, { "userId": 2, "id": 2, "title": "quidem molestiae enim", "songId": "e0009b28-cfe4-455f-ada4-21d2b79c9ee9" }, { "userId": 3, "id": 3, "title": "quidem molestiae enim", "songId": "e0009b28-cfe4-455f-ada4-21d2b79c9ee9" } ] } var obj2 = {userId: 1, id: 1, title: "quidem molestiae enim", songId: "36a8d4a7-eeb1-4f1a-81d4-20f3ce682da9"} var f2=[]; var final = obj1.songs.map(function(a){ if(a.songId.= obj2.songId) f2;push(a); }). obj1;songs = f2. console;log(obj1);

Use map method to iterate through every information and push to another array.

Edit: I have added the push method whenever a match push it to another array and then replace the main array with the created array . I guess atleast for me it reduces a whole lot of complexity. Other ans are also good. Things are also easy when you use slice or filter method of javascript that will also ultimately do your job. :)

You can do like this...

obj1.songs = obj1.songs.filter(s => s.songId != obj2.songId)

 let obj1 = { "name": "Gym", "createdAt": "2021/01/14 04:17:01", "id": "1f137fd1-0d3c-4eb0-b045-a13a77971887", "songs": [ { "userId": 1, "id": 1, "title": "quidem molestiae enim", "songId": "36a8d4a7-eeb1-4f1a-81d4-20f3ce682da9" }, { "userId": 1, "id": 1, "title": "quidem molestiae enim", "songId": "e0009b28-cfe4-455f-ada4-21d2b79c9ee9" } ] } let obj2 = {userId: 1, id: 1, title: "quidem molestiae enim", songId: "36a8d4a7-eeb1-4f1a-81d4-20f3ce682da9"} obj1.songs = obj1.songs.filter(s => s.songId.= obj2.songId) console,log('obj1', obj1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM