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)
Source: stackoverflow.com
Related Query
- How to compare and remove the object from array if value is same in Javascript?
- How to get object value from an array in the local storage and compare it with a new value?
- How to reduce and get value from object array using javascript
- How to compare each elements in an Array and group the ones that have same data (in my case date) ? JavaScript
- How to fetch the Promise object value from an array and use it
- How to compare object key as array value and assign a value to the variable?
- How can I group specific items within an object in the same array and delete them from the core array?
- How to do a foreach for an array with values that have to match specific key and the same value from another array
- How to find if one of the items have completed property with value false from an array of objects using javascript and react?
- How to find an object from recursive array of objects that is matching with the id using javascript and react?
- How do I compare two arrays of objects to see if they have the same ids and then return another value from the matching object?
- How to filter and sort the same array of object state in redux?
- ReactJS / Javascript How to remove the duplicate array from multi dimentional array?
- How to remove "[" and "]" signs from array in Javascript
- Javascript 2 arrays of objects , get a value from one array of objects and assign it to the other array of objects
- Get object array from axios and using return value call another axios function and append the output to first result
- How to remove selected index of an array from the DOM and reflect the change in React state?
- How would I prevent duplicates in array of objects? And if there is a object with same property, it should sume its value
- How to group arrays of objects by value and add the result to another array with same value at specific key
- How to create a Javascript object from an array of known keys, but with the values initialized to null?
- Javascript how to see if two objects with array of string contain the same value
- How to conditionally remove a value from a javascript object during a setState call in ReactJS?
- How to remove specific element from a array or object using ES6 spread operators and rest operators
- How to consecutively add values from one array to another and store the value in a new array in javascript?
- How to check and return value of state object array and use that to identify which object to take data from
- How to check if a value already exists in an array and remove that from array in react js?
- filter object and get all items that have the same value javascript
- How do I iterate over an array of objects match a common element from another array and return the key value for "name"
- React: How do I prevent the usage of useRef and useState to keep track of a same value in order to prevent useEffect from triggering?
- Couldn't calculate value of volume on basis of values from the same object nested in an array
More Query from same tag
- React event handlers not binding properly
- Delete index in a array - React.js
- How can I create a custom page with #react-admin without the menu sidebar like login page?
- "react-dnd-html5-backend" has no exported member 'HTML5Backend'
- Trying to Call Google Cloud Function, Where Do I Put The Function URL And /path/to/credentials.json in CloudFunctionsServiceClient?
- How do i make a component like this in react?
- React + Redux for a complex SaaS
- React, set body style conditionally by adding a class related to a state
- how to handle %20 in Link URL
- Unable to pass props correctly to react-countdown component
- MaterialUI - Indepdendent Grids next to each other?
- How to set Two inputs on same row in react native ?
- Unexpected token '<' in React Context Provider
- React hooks state variable not updating after rerender
- Which React Material-UI components add a spacing to my page (like a row class in Bootstrap)?
- Traversing an array of objects and comparing them
- Unable to compile react application as it says "Module not found"
- What is wrong with this simple react example?
- React Component with Scripts That Returns Nothing
- Why Reactjs is displaying all the Users Fullname at a time
- How to use ant design in server side rendering?
- How to reset state when route changes?
- Why is it that I can't use react and express library from global space(installed via npm i -g pkgName) but I can use react-scripts from global space
- Form Validation not working using react and StepWizard
- React with Typescript EventHandler type checking problem (onClick)
- implement auth route - with nested /signin and /signup routes
- How to make a conditionally recursive function return (null) in ReactJS?
- Typography in React Material-UI
- Trigger Marker on external element hover without map re-rendering React
- React - Maintain form wrapper state on input change