score:2
First off, you'll have to make sure if the ID types are correct. Users has Number type for IDs but attendingUsers has String type.
Let's say they're both the same type for the code below (I'm going with string).
You can turn the attendingUsers array into an array of strings with:
const attendingUsersIds = attendingUsers.map(attendingUser => attendingUser.id)
Then match the ids with:
const matchedUsers = users.filter(user => attendingUsersIds.includes(user.id))
If they're intended to not be the same type, you can use user.id.toString()
to turn the Number into a String or parseInt(attendingUser.id)
to turn the String into a Number.
score:2
We can use Array.map to create a new array of users, with a property isAttending added to each user.
We determine if they are attending by using Array.some to search for any matching attendee with the same id.
const users = [
{
id:1,
name:"John",
mail:"aaa@gmail.com",
},
{
id:2,
name:"Joe",
mail:"bbb@gmail.com",
},
{
id:3,
name:"Alice",
mail:"ccc@gmail.com",
}
]
const attendingUsers = [
{
id:"1",
name:"John",
mail:"aaa@gmail.com",
},
{
id:"2",
name:"Joe",
mail:"bbb@gmail.com",
}
]
const result = users.map(user => {
return { ...user, isAttending: attendingUsers.some(({id}) => id == user.id) };
});
console.log("Users (with attending property):", result);
Source: stackoverflow.com
Related Query
- How to check if property of an objects of array matches with one of the values in another array of object
- How to find if one of the items have completed property with value false from an array of objects using javascript and react?
- How do I iterate though an array of objects with keys and values in react and render them in the JSX( Both keys and Values)
- Check a list of checkboxes if input value matches one of the values in different array
- Check uniqueness with in nested object property values in array of objects
- I want to go though array and check if it matches with the values or not in React
- How to validate with Yup that in array of objects at least one of object keys has true value?
- How to check if array of objects that contains another array of object has property
- How to check if an Array with Object contains a Property
- How can one property of an object from an array be true if that property of other objects is false?
- How to map array of objects which i need to groupby date property in JSON format with react JS
- Filter the array of objects with optional values
- How to get the last children in a deeply nested array with objects
- How to check the values of an array and delete some properties based on the condition?
- How to change property of one object in an array of objects in React state?
- How to change values in array of objects with an onClick?
- How to get values from one array of objects into another array of objects
- 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 update an existing array of objects (add a new object to the array) with the useState hook?
- How to group values from an array of objects by same values with javascript?
- How can I break through React maps to display object that has two values one with an array other with string or number(result)
- How to consecutively add values from one array to another and store the value in a new array in javascript?
- How can I shuffle an array of objects on page render and map over the shuffled array without console warning of "two children with the same key"?
- How to find the count of some object property in array of objects using react and javascript?
- Best way to group an array with property of object and how to render the result
- Array of objects with values pass to useQuery in the custom hook and it become empty?
- How to check if object matches with values on checkbox click?
- So how can I sum all the values in all objects in an array which is coming from react redux?
More Query from same tag
- “react-infinite-scroll-component” Stopped working after one call (loadMore only gets called once)
- Conditionnal close tags ReactJS
- setInterval only executes once
- React: Reactstrap pagination active not working inside loop
- In react.js how can you asynchronously assign source attribute for video.js object
- React Props Changing Unexpectedly
- GraphQL returns data but undefined in code
- How to update meta tags in React.js?
- Updating a D3 Chart within React/Redux
- String is not assignable to keyof Readonly<T> typescript
- React onMouseEnter and onMouseLeave not behaving consistently
- Uncaught TypeError: (0 , _arrays2.default) is not a function
- How to set innerHTML jsx format
- Redux store doesn't change with the change of react state
- React On Screen Keyboard does not Trigger Change Event
- how to get value onchange event when state data in object
- ReactJS form validation when state is not immediately updated
- remove product from redux shopping cart
- How to use React Redux store in component that modifies and renders the state
- How can I add a user input with an output and be able to display using React Javascript/ Material UI
- -React Native, what is the way of passing state data from A.js to B.js
- How to update one item without affecting other arguments?
- Access to array in React
- How can I set a a state variable to an object in react?
- React render PDF file inside component based on API response
- I get two errors that I could not solve in return ESLint and destructing
- Why use redux-persist over manually persisting state to localStorage?
- async await syntax not catching any error on a jwt verification
- How to set Material UI Fab to Outline
- Can't seem to pass prop from component to component. (React/Redux)