score:0
You could use 'array.concat([])' to merge two array objects together. See bellow example.
let UserId = [{ id: 1, name: "john" }, { id: 2, name: "micheal" }];
const userCredentials = [{ id: 1, country: "de" },{ id: 1, country: "us" }];
const newArray = UserId.concat(userCredentials);
Since you have defined UserId as a const you cannot change it. So you have to make it to let or var to modify the variable.
score:4
Basically you need to map thru 1 array and find if each object in the array exists in another array and use spread operator and return the merged object in map callback
Use the code below:
// option 1 - if you know the keys of the object
let merged = UserId.map(u => {
const user = userCredentials.find(uc => uc.id === u.id);
if (user) u["country"] = user.country;
return u;
});
// option 2 - generic merge
let merged2 = UserId.map(u => {
const user = userCredentials.find(uc => uc.id === u.id);
if (user) return { ...u, ...user };
return u;
});
Source: stackoverflow.com
Related Query
- Updating the values in an array of objects in react js
- 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)
- So how can I sum all the values in all objects in an array which is coming from react redux?
- How can I store previous values instead of storing the value in array of objects in react state
- react js filter through an array of objects and compare values within to get the closest value to a variable
- setState an array of Objects updating two values in the Object accordingly of the id
- Updating the array object in React state using immutability helper
- Append array of values to the current array in a state in React JS
- mapping an array of objects and changing the value with on onClick in React
- React State update a nested array with objects based on the id when iterated
- Adding up the values of an array of objects javascript
- Fastest way to sort array of objects on the basis of nested key values
- get all values from array of objects in react typescript
- React search the text in an array of objects on Onchange of input
- React Material-UI Select using an array of objects as the source
- How to update the array of objects using onChange handler in React JS?
- Updating only one property inside an array of objects react
- How to update specific value when the state is an array of objects - React
- React updating single object of an array of objects - map vs entire array
- While updating the variable with index, all the objects with the same key but different index also changing in react js
- Mirroring the position when moving objects from the array to the array to another object in React
- When I am updating an array in React useState, it keeps defaulting to the initial state. What am I doing wrong?
- Filter the array of objects with optional values
- how do i update state in react for updating the value of an object inside an array using array.map function
- Objects are not valid as a React child when setState of an array at the end of a promise chain
- React js - Updating state with array values and a line break
- React useState not updating array of objects
- How to check if property of an objects of array matches with one of the values in another array of object
- how to split the values in map array in react
- How to sort the date form to latest to the oldest in array of objects react js
More Query from same tag
- Check if all setState methods are completed in componentDidMount
- React - call function in child component if parent state changes
- Cleaning component states useEffect
- Ways to display a React app in ASP.NET Core MVC
- using device camera for capturing image in reactjs
- Atom package for HTML support in React JavaScript files
- React Hook useCallback has a missing dependency:
- Authorization header not present on page refresh
- React Bootstrap get value from form on submit
- Hosting Apple verification file with NextJS
- Integration/Acceptance testing of a ReactJS App
- How Do I Pass on an Array of Objects using "forEach" Function?
- Got troubles to deploy on Heroku
- react router changes url but not navigating to page
- React-Native: Show loading screen till the webview is loaded
- Is there a way I can map the keys of an object to render keys of a nested object as a list under its respective category
- Website only changes after reload - react router
- How to send component's state as payload to store in React redux
- Getting data from Facebook API React
- How to change default Loading Spinner for Ant Design Pro
- Axios promise in a loop
- how to get data from localstorage and set state react
- onChange setState is rerendering all components?
- Loading react table through API call
- webpack4 react jsx can't render img
- Rendering from a child to a parent using refs and portals in React
- How to get the webshim polyfill library working in React?
- I don't know how to get data from fetch correctly
- Is it possible to use props in React.hooks?
- How to save Redux Store as a downloaded file and then load it back?