I have two arrays of object data that is the unequal length I just want to create a third array that is given below
first input array data
let firstArray = [{
id: '0',
name: 'shanu',
age: '21'
},
{
id: '1',
name: 'bhanu',
age: '21'
},
{
id: '2',
name: 'chary',
age: '21'
},
]
second set of array of data
let secondArray = [{
id: '10',
name: 'shanu',
age: '28'
},
{
id: '11',
name: 'raanu',
age: '29'
},
{
id: '12',
name: 'chary',
age: '30'
},
{
id: '15',
name: 'kushal',
age: '31'
},
]
output array- if second set name watch with first set of data replace the second set id with first set it
let outputArray = [{
id: '0',
name: 'shanu',
age: '28'
},
{
id: '11',
name: 'raanu',
age: '29'
},
{
id: '2',
name: 'chary',
age: '30'
},
{
id: '15',
name: 'kushal',
age: '31'
},
]
I'm not able to figure out how to map and filter together two different length of carry of array of object
CodePudding user response:
You could map the secondArray
and replace the id
if name
is found in firstArray
:
let firstArray = [
{ id: '0', name: 'shanu', age: '21' },
{ id: '1', name: 'bhanu', age: '21' },
{ id: '2', name: 'chary', age: '21' }
]
let secondArray = [
{ id: '10', name: 'shanu', age: '28' },
{ id: '11', name: 'raanu', age: '29' },
{ id: '12', name: 'chary', age: '30' },
{ id: '15', name: 'kushal', age: '31' }
]
let thirdArray = secondArray.map((item) => {
let found = firstArray.find((i) => i.name === item.name)
return { ...item, id: found ? found.id : item.id }
})
console.log(thirdArray)
CodePudding user response:
How about this:
Loop through the 2nd array, look for name matches in the 1st array, If a match exists, update the 2nd array.
for (i=0;i<secondArray.length;i ){
for(j=0; j<firstArray.length; j ){
if(secondArray[i].name == firstArray[j].name){
secondArray[i].id = firstArray[j].id
}
}
outputArray[i] = secondArray[i]
}
The output array would be the same as your 2nd array. If you don't want to change anything in your 2nd array, you could copy the array first and then make the changes in the double for-loop on the output array instead of the second array.
CodePudding user response:
I would make a look up based on the name. I would then loop over the objects and if the name exists, update the age. If the name does not exist, add the item to the array.
const firstArray = [{
id: '0',
name: 'shanu',
age: '21'
},
{
id: '1',
name: 'bhanu',
age: '21'
},
{
id: '2',
name: 'chary',
age: '21'
},
]
const secondArray = [{
id: '10',
name: 'shanu',
age: '28'
},
{
id: '11',
name: 'raanu',
age: '29'
},
{
id: '12',
name: 'chary',
age: '30'
},
{
id: '15',
name: 'kushal',
age: '31'
},
]
// create a look up object based off the name
const data = firstArray.reduce((obj, item) => {
obj[item.name] = { ...item } ;
return obj;
}, {});
secondArray.forEach(item => {
// does it exist? if yes, set the age
if (data[item.name]) {
data[item.name].age = item.age;
} else { // if no copy over the object
data[item.name] = { ...item };
}
});
//Get your array of Objects
console.log(Object.values(data));
CodePudding user response:
The below may be one method to achieve the desired objective:
Code Snippet
const updateId = (arr1, arr2) => ( // arr1 has the up-to-date / current "id"
arr2.map(({name, ...rest}) => (
arr1.some(ob => ob.name === name)
? {name, ...rest, id: arr1.find(ob => ob.name === name)?.id}
: {name, ...rest}
))
);
const firstArray = [{
id: '0',
name: 'shanu',
age: '21'
},
{
id: '1',
name: 'bhanu',
age: '21'
},
{
id: '2',
name: 'chary',
age: '21'
},
]
const secondArray = [{
id: '10',
name: 'shanu',
age: '28'
},
{
id: '11',
name: 'raanu',
age: '29'
},
{
id: '12',
name: 'chary',
age: '30'
},
{
id: '15',
name: 'kushal',
age: '31'
},
];
console.log(updateId(firstArray, secondArray));
Explanation
- iterate over
arr2
(ie, 'secondArray') - for
name
, check ifarr1
(ie, 'firstArray) has a match - if yes, update the id by using
.find
- else, keep the info in
arr2
iteration as-is