1
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)

6 Answers 6

2

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)

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

Comments

1

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)

2 Comments

It feels like the array containing {yes: 0, no: 0} is unnecessary. Given that the request was for something like what was given, I feel like that grants licence to simplify the structure a little.
@Wyck hmm i didnt see the something like part. anyway the other solutions show the non redundant structure
1

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}

Comments

0

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

Comments

0

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

Comments

0

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

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.