score:0

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

score:0

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. :)

score:0

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)


Related Query

More Query from same tag