0

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)
   );
});

2
  • 1
    It's not clear to me what you want. The sum of the fields you named in each of the three example objects of the top array are: 9, 6, 7 for elements 0, 1, and 2 respectively of the array of objects. But you show an expected output consisting of an object containing the three named properties and their values (which seem unrelated to the example array), and no sum. Could you show the exact output you need from the example array given? Commented Nov 7, 2022 at 2:31
  • The arithmetic in your example result is inconsistent with the example inputs. 2.0 + 3.0 + 4.0 ≠ 4.0, 3.0 + 1.0 + 1.0 ≠ 6.0, 4.0 + 2.0 + 2.0 ≠ 7.0 I hope people will still get the idea that it's supposed to be the sum. I'm just saying the question would be better if it used the correct sums. People who are into programming are often quite particular about numerical details and consistency. Many of us have made a career of it. Commented Nov 7, 2022 at 3:22

3 Answers 3

3

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 )

Sign up to request clarification or add additional context in comments.

2 Comments

Is there any way to make keys (s.job_security) dynamic?
@RAHULKUNDU I have changed my answer for more "dynamic" ...
0

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;
}, {})

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.