2

I have an Object with nested key value pair as:

Products:{
 Clip:{
   today_entry: 0,
   today_sold: 0
 },
 Necklace:
 {
   today_entry: 0,
   today_sold: 2
 }
}

I want to loop through the Objects Clip ad Necklace and group the values as per their inner keys i.e. today_entry, today_sold into the format:

{
 today_entry : [0,0],
 today_sold : [0,2]
}

I tried doing it using Object.entries but since it is nested I'm not able to get the inner keys. Could anyone please help me? Thank you.

2
  • Be careful that Objects are not ordered, Clip does not come necessarily prior to Necklace (unless you give some ordering criteria that you did not tell us) so there is no meaning wanting an output with arrays (that are, well, ordered) Commented Dec 8, 2020 at 9:16
  • 1
    This is the api response received which I want to present it in a combination chart, so order of the objects doesn't really matter @Zzirconium Commented Dec 8, 2020 at 10:58

3 Answers 3

4

You can use reudce:

const products = {
 Clip:{
   today_entry: 0,
   today_sold: 0,
 },
 Necklace:
 {
   today_entry: 0,
   today_sold: 2,
 },
};

const result = Object.keys(products).reduce((ac, key) => ({
 today_entry: [ ...ac.today_entry, products[key].today_entry],
 today_sold: [ ...ac.today_sold, products[key].today_sold],
}), { today_entry: [], today_sold: []});

console.log(result);

In case the order of the values in the arrays are important you should also sort the keys the way you want.

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

2 Comments

Very good solution. What does this ...ac.today entry?
@Alex it is called spread operator which in this case spreads the values from an array into the other newly created array.
3

It is possible to use reduce() method to create an object with Object.values() to get values from object:

const result = Object.values(products).reduce((a, c) => {
    for (const key in c) {
       a[key] = a[key] || [];
       a[key].push(c[key]);
    }
    return a;
},{});

An example:

let products  = {
 Clip:{
   today_entry: 0,
   today_sold: 0
 },
 Necklace:
 {
   today_entry: 0,
   today_sold: 2
 }
}

const result = Object.values(products).reduce((a, c) => {
  for (const key in c) {
a[key] = a[key] || [];
a[key].push(c[key]);
  }
  return a;
},{});



console.log(result)

2 Comments

very elegant solution. But again I think the OP wanting this out as an array is a mistake as there is no guarantee Products keys will be reduced in a deterministic order.
@Zzirconium thanks for the kind words! It means to me a lot! Let's wait OP to know what he actually wants! :)
1
const products = {
 Clip:{
   today_entry: 0,
   today_sold: 0
 },
 Necklace:
 {
   today_entry: 0,
   today_sold: 2
 }
}

const result = {};

for(item in products) {
  for (action in products[item]) {
    result[action]= result[action]||[];
    result[action].push(products[item][action])
  }
}

console.log(result)
//{ today_entry: [ 0, 0 ], today_sold: [ 0, 2 ] }

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.