0

Array of dictionaries should be converted simpler form.

 data = [{A:1},{B:2},{C:3}] 
 data = {A: 1, B: 2}
 data = ["0":{ A : 1, B : 2 , C : 3}]

Both are completely different datasets. I'm trying to map it also like below format. The above should become like

 data = [
  {
    name: "A",
    y: 1
  },
  {
    name: "B",
    y: 2
  },
  {
    name: "C",
    y: 3
  }
];

I tried this following approach but it's wrong

name = {}
data.forEach(function(k,x){
    return name['name'] = k , name["y"] = x 
})

Please suggest me a better approach.

1
  • I'm not sure I understand in your code sample above, where it says and data = {A: 1, B: 2}. Can you clarify? Commented Jul 27, 2018 at 22:33

5 Answers 5

1

map each object's entries to extract the key and the value, and return an object with name and y keys:

const data = [{A:1},{B:2},{C:3}]
const output = data.map(item => {
  const [name, y] = Object.entries(item)[0];
  return { name, y };
});
console.log(output);

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

2 Comments

How do I do the same thing for 3d data format when the whole dictionary is the value of a single key?
Like what? Give an example of an input and an output, in a new question (avoid chameleon questions) If it's just your data = ["0":{ A : 1, B : 2 , C : 3}], then just select the first array item first
0

If the keys (A, B, etc) are guaranteed to be unique throughout the array, then everything becomes simpler.

const data = [{A:1},{B:2},{C:3}];
const merged = Object.assign({}, ...data);
const newData = Object.entries(merged)
  .map(([name, y]) => ({ name, y }));
console.log(newData);

However, if the keys aren't guaranteed unique, then refer to CertainPerformance's answer.

Comments

0
you can implement like this

 var data = [{A:1},{B:2},{C:3}];
 
 var reformattedArra = data.map(obj => {
  let val = {};
  val.name = Object.keys(obj)[0];
  val.y = obj[Object.keys(obj)[0]];
  return val;
 })
 
 console.log(JSON.stringify(reformattedArra));

Comments

0

I would say, use Object.keys() which is widly supported

let data = [{A:1},{B:2},{C:3}];

data = Object.assign({}, ...data);

data = Object.keys(data).map(key => ({ name: key, y: data[key] }));

console.log(data);

4 Comments

Note that Object.assign(...data) mutates the first element of the input array, which can cause weird, hard-to-detect bugs in your code. To avoid this, I recommend using Object.assign({}, ...data).
@greim Used Object.create() and there is no such issue
That isn't how Object.create() works. It throws an error now. I think you were on the right track before.
Thanks @greim for reporting error, it should work but you are right, assign is better choice here
0

You yould could chekc the data format and if it is not an array, build one and reduce the array by taking the objetcs and create for each key/value a new object for the result set.

function simple(data) {
    return (Array.isArray(data) ? data : [data]).reduce((r, o) => [...r, ...Object.entries(o).map(([name, y]) => ({ name, y }))], []);
}

console.log(simple([{ A: 1 }, { B: 2 }, { C: 3, D: 4 }]));
console.log(simple({ A: 1, B: 2 }));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.