score:2
Accepted answer
you cannot compare objects you should check that an element with that id doesn't exists in the other array
here i used some
that returns a boolean if he can find a match
const a = [{
id: 1
}, {
id: 2
}];
const b = [{
id: 1
}, {
id: 2
}, {
id: 3
}];
const array3 = b.filter(obj => !a.some(({id}) => obj.id === id));
console.log(array3)
score:0
in your case, the following code gives all unique objects as an array, based on the id.
const a = [{
id: 1
}, {
id: 2
}];
const b = [{
id: 1
}, {
id: 2
}, {
id: 3
}];
const array3 = b.filter(objb => a.some((obja) => objb.id !== obja.id));
console.log(array3)
score:0
a different approach with a symmetrically result.
const
take = m => d => o => m.set(o.id, (m.get(o.id) || 0) + d),
a = [{ id: 1 }, { id: 2 }],
b = [{ id: 1 }, { id: 2 }, { id: 3 }],
map = new map(),
add = take(map),
result = [];
a.foreach(add(1));
b.foreach(add(-1));
map.foreach((v, id) => v && result.push({ id }));
console.log(result);
Source: stackoverflow.com
Related Query
- How to get unique element from two arrays in js?
- How do I combine two arrays in react to get a new one with all of the items from the previous two?
- How to get "key" prop from React element (on change)?
- How to get "key" prop from React element (on change)?
- Javascript Redux - how to get an element from store by id
- How to get the Id of an element from <select> in React?
- how to get unique values from a list of objects?
- How can i get an input element from a component in reactjs
- How get data from the state and edit the element in the form?
- How to get same ID from button that has children elements inside, prevent child element click?
- How do I extract two arrays from a JSON with nested objects
- How to get First element from a map that includes something?
- How do I get data set from a tr element
- How to get element from array in new line? react
- How to get react element from event.target
- How to get the value of ckeckbox element from a form submission in React.js?
- How to get element height and width from ReactNode?
- How a child component can get several props/data from two separate parent components
- React: How to get the ref height of an element coming from the children with dynamic height?
- How can I get one single unique data from Database for each one component?
- How do I get a variable from a graphQl query into a pseudo element in either inline styles or styled components
- How to get id of a button in a form with two buttons both from type submit
- How to get a single data element from the database with Redux and React?
- how do i pass array from select html element component to two text components that have different values in the array
- how to get new lines "\n" from string also reflected on the paragraph element
- How to get data from two endpoints in 1 component?
- How to get two different values from a select?
- how can I get the first unique ID element and last unique ID element in an array React js
- Reactjs how to get value from selected element
- How to get next and previous five element of from an array?
More Query from same tag
- In React how do I use a conditional with rendering?
- Conditionally Render CSS Based On Route (React)
- Front-end JavaScript pattern to ignore previous request responses
- Getting error "PropTypes is not defined" in React
- React lazy rendering on slow children
- Prefill form data in React
- Sanity newly added field is not showing on queries
- How to use react-router's Redirect Component to redirect to a dynamically generated route?
- how to wait for child components rendered
- React use custom hook function
- How can i manipulate DOM inside the onScroll event in ReactJS?
- How to set a variable in a parent component from a child component
- could not create react project
- Why setState() isn't working in React in my case?
- redux-simple-router + react-router error "route state does not exist"
- TypeError: Cannot read properties of undefined (reading 'map') ReactJs Getting data from a file
- Uncaught TypeError: Cannot read property 'func' of undefined
- React + d3.js - svg rerenders on top of previous one with new props
- tailwind css not working between components
- Opening a new image onclick in react
- Why is my API key visible when using next.js with environment variables?
- How to use country flags in node and react
- Set history dynamically without page reload
- Component definition is missing display name react/display-name
- How to remove from am array in React/Redux
- Optional property on a component?
- Filtering data from AWS Amplify Graphql API
- Open one Submenu at a time React js
- react I want to move the scroll position to top
- Looping over on array inside a React component defined as a class