score:0
To loop that we first need to loop the first array then find element with title in the second array and add the JSON key we want to add as well
array1.map(elem => {
const arr2WithTitle = arr2.find((data)=>data.name === elem.name);
return arr2WithTitle.title ? data.title =arr2WithTitle.title: arr2WithTitle.title = "none"
})
score:1
Your two arrays don't correspond to a useful output based on your requirements
Stating them a bit clearer
- If a value of the property
name
inarray2
is not any of the values of the array elements ofarray1
then do not add thearray2
element to the new array - If the value of the property
name
inarray2
is inarray1
AND the array element doesn't have the property title, add this to the array element.
Based on that criteria, you will get an empty array based on your arrays array1
and array2
.
const array1 = [
{ name: "Jack", age: 54, title: "IT Engineer" },
{ name: "Josephine", age: 54, title: "chef" },
{ name: "Cortez", age: 34, title: "driver" }
];
const array2 = [
{ name: "Alex", age: 24, },
{ name: "Janvier", age: 24, title: "IT Engineer" }
];
const array1_Values = array1.map(elem => elem.name);
const newArray = array2.filter(elem => {
if (array1_Values.includes(elem.name) && !elem.hasOwnProperty('title')) {
elem.title = 'none';
return elem;
}
})
console.log(newArray); // Prints [] to console
Source: stackoverflow.com
Related Query
- How can I compare two arrays of objects and add new key to an objects in the second array
- How can I use Promise.all on an array of objects and then reassign them to the relevant key in a new object?
- How to group arrays of objects by value and add the result to another array with same value at specific key
- How can I add new a new object with key and values pairs to the existing state in class based component?
- 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 would you compare two arrays and filter between the two arrays?
- How can I add a new item and render it without the timeout? React
- How can I keep previous state and add a new state to the previous state in Redux reducers?
- Comparing two arrays of nested objects and return new array of objects if compared values are not the same in javascript
- How to compare two array of objects in javascript and update the value?
- How can we compare objects by value stored inside two arrays of objects in JavaScript?
- compare two arrays of Objects in JavaScript and if true save new items to MongoDB
- Compare two arrays and show the items of the first array that have the key from the second array
- How can I access two separate images in an array of objects and assign it to a third object inside the same array?
- How can I compare two arrays to create a new one?
- How to display the last page when user clicks on add new row,so that user can view the newly added row using react js and prime react
- How i can limit the items in the FlatList and add load more?
- How can I map over two arrays at the same time?
- How can I drag and drop objects into a Mapbox map from outside the map?
- Firestore - How can I add the Document ID and their data in my state?
- How can I check two arrays at the same index?
- ReactJS - How to compare/filter matching id's of two arrays and use the result in a double search feature?
- Comparing two arrays and adding objects to the other one
- How do I combine two arrays in react to get a new one with all of the items from the previous two?
- How I can loop through the similar objects in state and push them into object?
- How would I compare two dates in a form and validate them? The second one shouldn't be before the first one
- How to construct an Array to first compare and then add the input
- React-Router: How do I add a new component and route to the onboarding steps on a wizard?
- On an HTML form with two buttons, how can I get one button to submit the form and the other one not?
- how can i extract arrays from one array & convert them in object and put them in a new array?
More Query from same tag
- Changing React state taking time so it takes old state in function logic
- setState counter showing up as NaN
- Image doesn't show up in react JS and CSS
- Set URL to hyperlink with Rest call in React
- React router doesn't show anything on the new route
- React component retrieves props just once, goes undefined when refreshed
- i get this error Error: Cannot find module '../list/songQueue.jpg' ,using json-server i am passing path of my image file
- React - useState returns initialized value even after manipulating it
- What does the "single source of truth" mean?
- Errors with clipboard.js in React component?
- React render *.md via custom component
- How to setup a generic interface for the state when using React useReducer with typescript
- Webpack Sytax Error Unexpected "::" from React code
- How to stop an activity sending to directline webchat from React Js code
- How to write an interceptor for react router?
- Getting SyntaxError: Unexpected token export when trying to import a function to test with Mocha
- How to correctly wait for Translation with react-i18next
- Strange Unresolved dependencies to 'vscode' when wrapping monaco-editor and monaco-languageclient as a react component
- Loading component appears on top of the page
- React: Audio does not play
- react-axios handle error don't get the message from the server
- React, Nothing was returned from render
- Apex chart doesn't display immediately
- Interval running as a counter not being reset on button click
- How to make React page render only change component instead of the whole page?
- React web app route not loading on mobile browser
- React Class to Function components
- Rendering A List in React
- Component not reloaded with react hot loader
- How to stop react re-rendering component, if part of the state changes?