This question already has answers here:
Aggregating object values of JavaScript arrays?
(2 answers)
Closed 4 years ago.
I have an Array of objects, for example:
[{
name: 'mike',
id: 3312,
points: 2,
summary: 'example',
}, {
name: 'andrew',
id: 4123,
points: 1,
summary: 'example',
}, {
name: 'mike',
id: 0522,
points: 5,
summary: 'example',
}]
I need to sum the points for each person, the problem I´m facing is there are more than 50 different person names so I can´t do something like this
for (let i = 0; i < issues.length; i++) {
if (issues[i].name == 'mike') {
//////////////////////
}
}
Because the API in future can return a new Person.
You can reduce into an object indexed by name:
const input = [{
name: 'mike',
id: 3312,
points: 2,
summary: 'example',
},
{
name: 'andrew',
id: 4123,
points: 1,
summary: 'example',
},
{
name: 'mike',
id: 0522,
points: 5,
summary: 'example',
}];
const points = input.reduce((a, { name, points }) => (
Object.assign(a, { [name]: (a[name] || 0) + points })
), {});
console.log(points);
If you do not want to use reduce and keep things simple you can still use a forEach loop as below:
const input = [{
name: 'mike',
id: 3312,
points: 2,
summary: 'example',
},
{
name: 'andrew',
id: 4123,
points: 1,
summary: 'example',
},
{
name: 'mike',
id: 0522,
points: 5,
summary: 'example',
}];
var res = {};
input.forEach((obj) => {
res[obj.name] = res[obj.name] ? res[obj.name]+obj.points : obj.points;
});
console.log(res);
Related
Consider the following two arrays:
[
{
id: jhz,
name: 'John',
eyes: 'Green',
description: 'Cool guy',
},
{
id: mbe,
name: 'Mary',
brand: 'M&B',
text: 'Something',
}
]
[
{
id: jhz,
name: 'John',
eyes: '',
},
{
id: mbe,
name: 'Mary',
},
{
id: 'beh',
name: 'Bernard',
}
]
First array may have any kind of key value pairs, but it will always have the key id and name. I want to merge the two arrays by taking id and name into account and preserving them, while merging everything else and replacing them with data from the first array if any keys duplicate.
Also tricky part - the merged array needs to follow the order of the second array.
So in this example the result I'm looking for is:
[
{
id: jhz,
name: 'John',
eyes: 'Green',
description: 'Cool guy',
},
{
id: mbe,
name: 'Mary',
brand: 'M&B',
text: 'Something',
},
{
id: 'beh',
name: 'Bernard',
}
]
you can do something like this using Array.map
const data1 = [{
id: 'jhz',
name: 'John',
eyes: 'Green',
description: 'Cool guy',
},
{
id: 'mbe',
name: 'Mary',
brand: 'M&B',
text: 'Something',
}
]
const data2 = [{
id: 'jhz',
name: 'John',
eyes: '',
},
{
id: 'mbe',
name: 'Mary',
},
{
id: 'beh',
name: 'Bernard',
}
]
const result = data2.map(d => ({...d, ...(data1.find(d1 => d1.id === d.id && d1.name === d.name) || {})}))
console.log(result)
This question already has answers here:
Merge two array of objects based on a key
(23 answers)
Closed 1 year ago.
This is two object. One is 'major'. Other one is 'marjorModel'.
const major = [
{
id: 1,
subject: 'math',
title: 'no good'
},
{
id: 2,
subject: 'science',
title: 'good'
}
]
const majorModel = [
{
id: 1,
amount: 23,
date: '2022-03-01'
},
{
id: 3,
amount: 26,
date: '2022-03-01'
}
]
and I want to merge new one
example)
const newObj = [
{
id: 1,
subject: 'math',
title: 'no good',
amount: 23,
date: '2022-03-01'
},
{
id: 2,
subject: 'science',
title: 'good',
amount: 26,
date: '2022-03-01'
}
]
I don't know how to merge different of other object.
please let me know!!
You can use reduce
major.reduce((acc, v, i) => {
acc.push({
...v,
...majorModel[i]
});
return acc;
}, []);
I have array objects:
[
{
id: 1,
name: ABC,
age: 10
},
{
id: 2,
name: ABCXYZ,
age: 20
},
{
id: 3,
name: ZYXCNA,
age: 30
},
...
...
....more
]
and array is value of id in above array object:
[ 1, 2]
then i want a array objects not have value of id in above array objects.
Ex: Array objects i will receive in here is
[
{
id: 3,
name: ZYXCNA,
age: 30
},
{
id: 4,
name: ABCDX,
age: 30
}
]
i have using 3 for loop in here to slove this problem but i think that can have smarter way.
Like using reduce in here, but i still not resolved. Can anyone help me? Thanks so much.
You can do this using Array.filter & Array.includes.
const source = [
{
id: 1,
name: 'ABC',
age: 10
},
{
id: 2,
name: 'ABCXYZ',
age: 20
},
{
id: 3,
name: 'ZYXCNA',
age: 30
},
{
id: 4,
name: 'ABCDX',
age: 30
}
];
const input = [ 1, 2 ];
const output = source.filter(({ id }) => !input.includes(id));
console.log(output);
Yo don't need any explicit loops at all, you're just filtering
var input = [
{
id: 1,
name: "ABC",
age: 10
},
{
id: 2,
name: "ABCXYZ",
age: 20
},
{
id: 3,
name: "ZYXCNA",
age: 30
},
];
var ids = [1,2];
var result = input.filter(x => ! ids.includes(x.id));
console.log(result);
I have an array of object arrayOfUsers as below and i want to filter that dynamically based on the object filterValues. If i remove name from filterValues the code should filter the array with age only and if i add id also to filterValues it should filter the array with three properties with out directly specifying key in the filter code and the key should capture from filterValues. I would like to get a solution for this using callback function. Appreciate for the solutions.
const arrayOfUsers = [
{ id: 1, name: 'Sam', age: 33 },
{ id: 2, name: 'Ram', age: 34 },
{ id: 3, name: 'Raj', age: 32 },
{ id: 4, name: 'Som', age: 2 }]
const filterValues = { name: 'R', age: 3 }
Expected Output:
[
{ id: 2, name: 'Ram', age: 34 },
{ id: 3, name: 'Raj', age: 32 }
]
Here is the sample code where i am stuck on.
const test = (arr) => {
Object.keys(filterValues).map((key, i) => arr.filter((user) => user[key].includes(key[i])))
}
You could convert the properties to string and seach for a substring.
const
arrayOfUsers = [{ id: 1, name: 'Sam', age: 33 }, { id: 2, name: 'Ram', age: 34 }, { id: 3, name: 'Raj', age: 32 }, { id: 4, name: 'Som', age: 2 }],
filterValues = { name: 'R', age: 3 },
filters = Object.entries(filterValues),
result = arrayOfUsers.filter(user => filters.every(([key, value]) =>
user[key].toString().includes(value)
));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
This question already has answers here:
JavaScript merging objects by id [duplicate]
(18 answers)
How can I merge properties of two JavaScript objects dynamically?
(69 answers)
Closed 6 years ago.
Suppose we have a java script objects on array like this:
lets say i have a variable books having collection of book
var books ={
{
id: 1,
name: 'Physics'
}
{
id: 2,
name: 'Mathematics'
}
{
id: 3,
name: 'Chemistry'
}
}
And lets say every writer have a book name inside their info like this
var writers ={
{
id: 123,
name: 'William Jeff',
book: 'Physics'
},
{
id: 123123,
name: 'John Doe',
book: 'Mathematics'
},
{
id: 1212312323,
name: 'asd Doe',
book: 'Chemistry'
},
{
id:123123,
name: 'ASD DAS',
book:'Physics'
}
}
I want to merge these javascript objects to this one :
var result = {
{
id: 1,
name: 'Physics',
writers : {
{
id: 123,
name: 'William Jeff',
},
{
id:123123,
name: 'ASD DAS',
}
},
{
id: 2,
name: 'Mathematics'
writers : {
{
id: 123123,
name: 'John Doe',
}
},
}
{
id: 3,
name: 'Chemistry',
writers : {
{
id: 1212312323,
name: 'asd Doe',
},
},
}
}
I tried using underscore foreach but it gets terribly complicated like too much loop inside loop. I think there are certainly good way to do it :)
Is there anyway?
Thanks
You can iterate both objects if they have an id:
var books = { 1: { id: 1, name: "Math" }, ...}
var writers = { 1: { id:1, name: "John Doe", book: "Math"}, ...}
Or declare them as arrays:
var books = [{ id: 1, name: "Math" }, ...]
var writers = [{ id:1, name: "John Doe", book: "Math"}, ...]
Then you can do do the for-loops
var result = {}; //or as an array
for(bookKey in books){
var book = books[bookKey];
for(writerKey in writers){
var writer = writers[writerKey];
if(writer.book == book.name){
result[bookKey] = { //for an array: result.push(object)
id: book.id,
name: book.name,
writer: {
id: writer.id,
name: write.name
}
}
}
}
}
As an object result[1] will be the book with id 1.
As an array result[0] will be the book with id "X".