Home > Software engineering >  Count an array of objects
Count an array of objects

Time:07-08

const array = [
  { date: '2022-01-03', answer: 'yes' },
  { date: '2022-01-03', answer: 'no' },
  { date: '2022-01-03', answer: 'no' },
  { date: '2022-01-04', answer: 'yes' },
  { date: '2022-01-04', answer: 'yes' },
  { date: '2022-01-05', answer: 'yes' },
  { date: '2022-01-05', answer: 'yes' },
  { date: '2022-01-05', answer: 'yes' },
  { date: '2022-01-05', answer: 'no' },
]

And I'm expecting a result something like this:

{
    "2022-01-03" : [
        {
            "yes": 1,
            "no": 2
        }
    ],
    "2022-01-04" : [
        {
            "yes": 2,
            "no": 0
        }
    ],
    "2022-01-05" : [
        {
            "yes": 3,
            "no": 1
        }
    ]
}

I've been trying to do it this way but I can't find a way to make it work:

const array = [
  { date: '2022-01-03', answer: 'yes' },
  { date: '2022-01-03', answer: 'no' },
  { date: '2022-01-03', answer: 'no' },
  { date: '2022-01-04', answer: 'yes' },
  { date: '2022-01-04', answer: 'yes' },
  { date: '2022-01-05', answer: 'yes' },
  { date: '2022-01-05', answer: 'yes' },
  { date: '2022-01-05', answer: 'yes' },
  { date: '2022-01-05', answer: 'no' },
]

const result = array.reduce((prev, curr) => {
  prev[curr.date] = prev[curr.date] || [];
  prev[curr.date].push(curr.answer)
  return prev;
}, {});

console.log(result)

CodePudding user response:

Your reduce is close; it's just a matter of making sure you create objects within each date key to count the yes/no answers.

const array = [
  { date: '2022-01-03', answer: 'yes' },
  { date: '2022-01-03', answer: 'no' },
  { date: '2022-01-03', answer: 'no' },
  { date: '2022-01-04', answer: 'yes' },
  { date: '2022-01-04', answer: 'yes' },
  { date: '2022-01-05', answer: 'yes' },
  { date: '2022-01-05', answer: 'yes' },
  { date: '2022-01-05', answer: 'yes' },
  { date: '2022-01-05', answer: 'no' },
]

const result = array.reduce((acc, curr) => {
  if (!acc[curr.date]) {
    acc[curr.date] = { yes: 0, no: 0 }
  }
  acc[curr.date][curr.answer]  ;
  return acc;
}, {});

console.log(result)

CodePudding user response:

very close. just a step forward with a little tweak

const array = [
  { date: '2022-01-03', answer: 'yes' },
  { date: '2022-01-03', answer: 'no' },
  { date: '2022-01-03', answer: 'no' },
  { date: '2022-01-04', answer: 'yes' },
  { date: '2022-01-04', answer: 'yes' },
  { date: '2022-01-05', answer: 'yes' },
  { date: '2022-01-05', answer: 'yes' },
  { date: '2022-01-05', answer: 'yes' },
  { date: '2022-01-05', answer: 'no' },
]

const result = array.reduce((prev, curr) => {
  prev[curr.date] = prev[curr.date] || [{yes: 0, no: 0}];
  prev[curr.date][0][curr.answer] =1
  return prev;
}, {});

console.log(result)

CodePudding user response:

You can store the data in an object, then iterate through your array casting the new properties:

const newObj = {}
array.forEach((item) => {
  if(!newObj[item.date]) newObj[item.date] = { yes: 0, no: 0 };
  newObj[item.date][item.answer]  = 1;
});

console.log(newObj)

gives:

2022-01-03: {yes: 1, no: 2}
2022-01-04: {yes: 2, no: 0}
2022-01-05: {yes: 3, no: 1}

CodePudding user response:

It should work

 let result = {} 
    array.forEach((ele)=>{
        if(Object.keys(result).includes(ele.date)){
            result[ele.date][ele.answer]  ;
        }else{
            result[ele.date] = {yes :0 , no : 0 ,[ele.answer] : 1};
        }
    })

CodePudding user response:

Its Work

const data = [
  { date: '2022-01-03', answer: 'yes' },
  { date: '2022-01-03', answer: 'no' },
  { date: '2022-01-03', answer: 'no' },
  { date: '2022-01-04', answer: 'yes' },
  { date: '2022-01-04', answer: 'yes' },
  { date: '2022-01-05', answer: 'yes' },
  { date: '2022-01-05', answer: 'yes' },
  { date: '2022-01-05', answer: 'yes' },
  { date: '2022-01-05', answer: 'no' },
];

let result = {};
data.forEach(function(st, index){
    let yes = data.filter(x => x.date==st.date && x.answer=='yes').length;
    let no = data.filter(x => x.date==st.date && x.answer=='no').length;
    result[st.date] = {yes: yes, no: no};
});
console.log(result);

CodePudding user response:

One more way with forEach, destructuring and ??= operator

const process = (arr, track = {}) => {
  arr.forEach(
    ({ date, answer }) => (track[date] ??= { date, yes: 0, no: 0 })[answer]  
  );
  return track;
};

const data = [
  { date: "2022-01-03", answer: "yes" },
  { date: "2022-01-03", answer: "no" },
  { date: "2022-01-03", answer: "no" },
  { date: "2022-01-04", answer: "yes" },
  { date: "2022-01-04", answer: "yes" },
  { date: "2022-01-05", answer: "yes" },
  { date: "2022-01-05", answer: "yes" },
  { date: "2022-01-05", answer: "yes" },
  { date: "2022-01-05", answer: "no" },
];

console.log(process(data));

  • Related