I have an array of objects like below, where I want to calculate the sum of job_security, skill_development
and company_culture
these fields.
reviews = [
{
id: 1,
job_security: '2.0',
skill_development: '3.0',
company_culture: '4.0',
is_anonymous: false,
pros: 'Test 1...',
cons: "Test 1...",
created_at: '2022-10-19T19:07:18.000Z',
},
{
id: 2,
job_security: '3.0',
skill_development: '1.0',
company_culture: '2.0',
is_anonymous: false,
pros: 'Test 2...',
cons: "Test 2...",
created_at: '2022-10-19T19:07:25.000Z',
},
{
id: 3,
job_security: '4.0',
skill_development: '1.0',
company_culture: '2.0',
is_anonymous: false,
pros: 'Test 3...',
cons: "Test 3...",
created_at: '2022-10-19T19:07:35.000Z',
}
]
I am expecting an output like this, where total sums of all the fields will return as an object
{
job_security: '4.0',
skill_development: '6.0',
company_culture: '7.0',
}
This is what I have done :
const filteredKeys = [
'job_security'
'company_culture',
'skill_development',
];
reviews.forEach((review: any) => {
Object.keys(review).reduce(
(accu: any, key: string) => {
if (filteredKeys.includes(key)) {
const rating = Number(review[key]);
accu[key] = accu[key] || rating;
accu[key] = rating;
}
return accu;
},
Object.create(null)
);
});
CodePudding user response:
simply use Array.reduce() method :
const reviews = [{id: 1,job_security: '2.0',skill_development: '3.0',company_culture: '4.0',is_anonymous: false,pros: 'Test 1...',cons: "Test 1...",created_at: '2022-10-19T19:07:18.000Z',},{id: 2,job_security: '3.0',skill_development: '1.0',company_culture: '2.0',is_anonymous: false,pros: 'Test 2...',cons: "Test 2...",created_at: '2022-10-19T19:07:25.000Z',},{id: 3,job_security: '4.0',skill_development: '1.0',company_culture: '2.0',is_anonymous: false,pros: 'Test 3...',cons: "Test 3...",created_at: '2022-10-19T19:07:35.000Z',}];
const sums = reviews.reduce( (s,e,i,{[i 1]:eNext})=>
{
Object.keys(s).forEach( k => s[k] = e[k] );
if (!eNext) // for the last (no next element), change values to string
Object.keys(s).forEach( k => s[k] = s[k].toFixed(1) );
return s
}
,{ job_security: 0, skill_development: 0, company_culture: 0 });
console.log( sums )
CodePudding user response:
You could do something like this
const reviews = [
{
id: 1,
job_security: '2.0',
skill_development: '3.0',
company_culture: '4.0',
is_anonymous: false,
pros: 'Test 1...',
cons: "Test 1...",
created_at: '2022-10-19T19:07:18.000Z',
},
{
id: 2,
job_security: '3.0',
skill_development: '1.0',
company_culture: '2.0',
is_anonymous: false,
pros: 'Test 2...',
cons: "Test 2...",
created_at: '2022-10-19T19:07:25.000Z',
},
{
id: 3,
job_security: '4.0',
skill_development: '1.0',
company_culture: '2.0',
is_anonymous: false,
pros: 'Test 3...',
cons: "Test 3...",
created_at: '2022-10-19T19:07:35.000Z',
}
]
const filters = [
'job_security',
'company_culture',
'skill_development',
];
const output = reviews.reduce((acc, next)=> {
for (let filter of filters){
if (!(filter in next)) throw new Error(`Cannot find ${filter} in object`)
if (!(filter in acc)) acc[filter] = 0;
acc[filter] = next[filter];
}
return acc;
}, {})
CodePudding user response:
You can use reduce()
to do it
let reviews = [
{
id: 1,
job_security: '2.0',
skill_development: '3.0',
company_culture: '4.0',
is_anonymous: false,
pros: 'Test 1...',
cons: "Test 1...",
created_at: '2022-10-19T19:07:18.000Z',
},
{
id: 2,
job_security: '3.0',
skill_development: '1.0',
company_culture: '2.0',
is_anonymous: false,
pros: 'Test 2...',
cons: "Test 2...",
created_at: '2022-10-19T19:07:25.000Z',
},
{
id: 3,
job_security: '4.0',
skill_development: '1.0',
company_culture: '2.0',
is_anonymous: false,
pros: 'Test 3...',
cons: "Test 3...",
created_at: '2022-10-19T19:07:35.000Z',
}
]
let keys = [
'job_security',
'company_culture',
'skill_development',
];
let result = reviews.reduce((a,v) =>{
keys.forEach(k =>{
a[k] = (a[k]??0) ( v[k])
})
return a
},{})
console.log(result)