1

I would like add up all the duplicate values for each day and sort them by object key

    let meals = {
        'Sat Jul 11 2020': [{ fruit: "apple" }, { fruit: "apple" }, { fruit: "orange" }, { fruit: "apple" }],
        'Sat Jul 04 2020': [{ fruit: "orange" }, { fruit: "apple" }, { fruit: "orange" }],
        'Fri Jul 03 2020': [{ fruit: "orange" }, { fruit: "orange" }, { fruit: "apple" }, { fruit: "orange" }]
    }

    let keys = Object.keys(meals);

    let food = keys.map(item => {
        return meals[item].map((x) => {
            return x.fruit
        });
    });

    var sorted ={};

    food.forEach(i => {
        i.map((x) => {
            return [sorted[x] = (sorted[x] || 0) + 1];
        })

    });

JsFiddle

What i'm after is something like this:

  'Sat Jul 11 2020': [{apple: 3, oranges: 1}],
  'Sat Jul 04 2020': [{apple: 1, oranges: 2}],
  'Fri Jul 03 2020': [{apple: 1, oranges: 3}],

what I got at the moment is its adds up the values for all the days combined and it doesn't sort it by day

any ideas?

2 Answers 2

1

You could get the entries of the object and map new entries for getting an object.

Inside take the values array and count the fruits.

This approach returns an object instead of an obeject wrapped in an array.

let meals = { 'Sat Jul 11 2020': [{ fruit: "apple" }, { fruit: "apple" }, { fruit: "orange" }, { fruit: "apple" }], 'Sat Jul 04 2020': [{ fruit: "orange" }, { fruit: "apple" }, { fruit: "orange" }], 'Fri Jul 03 2020': [{ fruit: "orange" }, { fruit: "orange" }, { fruit: "apple" }, { fruit: "orange" }] },
    result = Object.fromEntries(Object
        .entries(meals)
        .map(([key, values]) => [
            key,
            values.reduce((r, { fruit }) => {
                r[fruit] = (r[fruit] || 0) + 1;
                return r;
            }, {})
        ])
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Comments

0

Really all you need is to reduce each meal value. You can accomplish this through the method shown earlier by Nina Sholz, or you can use a simple for of loop to get to every meal value:

for (const [key, value] of Object.entries(meals)) {
    // key is our date string
    // value is our array of fruits
}

Now that we have each key/value, we just need to reduce the array of fruits in value to one object:

const sorted = {}

for (const [key, value] of Object.entries(meals)) {
    sorted[key] = value.reduce((accumulator, current) => {
        const fruitOnObj = current.fruit; // "apple" or "orange" in this case
    
        // Add to our property on our sorted constant
        accumulator[fruitOnObj] = (accumulator[fruitOnObj] || 0) + 1;

        // Return our modified accumulator from the reducer function
        return accumulator;
  }, {})
}

JSFiddle

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.