1

I am trying to add the key to each so that I can be able to easy make a multi scatter plot in d3. . I am not sure how to do it.

EDIT: TO CLARIFY what I meant.

Data:

var dataOriginal = {
  Apples: [{"A":4,"B":null,"C":null,"D":2},  {"A":5,"B":null,"C":3,"D":2}],
  Oranges: [{"A":3,"B":1,"C":4,"D":4.3}],
  Jackfruit: [{"A":5,"B":4,"C":4,"D":3}],
  Avocado: [{"A":null,"B":33,"C":2,"D":9.66}],
  Pomegranate: [{"A":5,"B":3.5,"C":null,"D":6}]
}

Function:

const data = Object.keys(dataOriginal).map((key) => {
  const temp = {...dataOriginal[key]};
  temp.key = key;
  return temp;
});

Results:

0:
0: {A: 4, B: null, C: null, D: 2}
1: {A: 5, B: null, C: 3, D: 2}
key: "Apples"
__proto__: Object
1:
0: {A: 3, B: 1, C: 4, D: 4.3}
key: "Oranges"
__proto__: Object
2:
0: {A: 5, B: 4, C: 4, D: 3}
key: "Jackfruit"
__proto__: Object
3:
0: {A: null, B: 33, C: 2, D: 9.66}
key: "Avocado"
__proto__: Object
4: {0: {…}, key: "Pomegranate"}

Desired results

: {A: 4, B: null, C: null, D: 2, key: "Apples"}
1: {A: 3, B: 1, C: 4, D: 4.3, key: "Oranges"}
2: {A: 5, B: 4, C: 4, D: 3, key: "Jackfruit"}
3: {A: null, B: 33, C: 2, D: 9.66, key: "Avocado"}
4: {A: 5, B: 3.5, C: null, D: 6, key: "Pomegranate"}
5: {A:5,B:null,C:3,D:2, key: "Apples"}
4
  • do you want to add the nested properties by key? Commented Mar 3, 2020 at 14:02
  • 2
    const temp = {...dataOriginal[key][0]}; is specifying the first item in those arrays. you need to loop through dataOriginal[key] if you want all items Commented Mar 3, 2020 at 14:03
  • @mrben522 I have edited to make more sense. sorry Commented Mar 3, 2020 at 14:24
  • @NinaScholz yes. I have edited to add more context Commented Mar 3, 2020 at 14:25

2 Answers 2

2

The reason why {"A":5,"B":null,"C":3,"D":2} is missed is because, index 0 is hardcoded in the code. const temp = {...dataOriginal[key][0]};

Alternate solution:

var dataOriginal = {
  Apples: [{"A":4,"B":null,"C":null,"D":2},  {"A":5,"B":null,"C":3,"D":2}],
  Oranges: [{"A":3,"B":1,"C":4,"D":4.3}],
  Jackfruit: [{"A":5,"B":4,"C":4,"D":3}],
  Avocado: [{"A":null,"B":33,"C":2,"D":9.66}],
  Pomegranate: [{"A":5,"B":3.5,"C":null,"D":6}]
}

const myData =[]
Object.keys(dataOriginal).map((key) => {
  for (let i = 0; i < dataOriginal[key].length; i++) {
    myData.push({...dataOriginal[key][i], key})
  }
})

console.log(myData)

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

1 Comment

This was very helpful, was just looking for this solution as well.
1

You need to reduce the object to get a single object with added values.

const
    addByKey = array => array.reduce((a, b) => {
        Object.entries(b).forEach(([k, v]) => a[k] = (a[k] || 0) + v);
        return a;
    }, {}),
    dataOriginal = { Apples: [{ A: 4, B: null, C: null, D: 2 }, { A: 5, B: null, C: 3, D: 2 }], Oranges: [{ A: 3, B: 1, C: 4, D: 4.3 }], Jackfruit: [{ A: 5, B: 4, C: 4, D: 3 }], Avocado: [{ A: null, B: 33, C: 2, D: 9.66 }], Pomegranate: [{ A: 5, B: 3.5, C: null, D: 6 }] }
    data = Object.keys(dataOriginal).map((key) => ({ ...addByKey(dataOriginal[key]), key }));

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

For getting single object with same keys, you could map the objects, add the key and get a flat array.

const
    dataOriginal = { Apples: [{ A: 4, B: null, C: null, D: 2 }, { A: 5, B: null, C: 3, D: 2 }], Oranges: [{ A: 3, B: 1, C: 4, D: 4.3 }], Jackfruit: [{ A: 5, B: 4, C: 4, D: 3 }], Avocado: [{ A: null, B: 33, C: 2, D: 9.66 }], Pomegranate: [{ A: 5, B: 3.5, C: null, D: 6 }] }
    data = Object
        .keys(dataOriginal)
        .flatMap(key => dataOriginal[key].map(o => ({ ...o, key })));

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

3 Comments

Your results are the same is the "incorrect" example in the question. OP is looking for each object in the original data to be merged into a new one-dimensional array.
I am understanding your answer. Do you mind explaining. I have also editted the question to add more clarity.
@Kabooki, which one do you want?

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.