'convert object in to array of json with given keys [closed]

I have object like

{ labels: ["city A", "city B"], data: ["Abc", "Bcd"] };

I want to convert above object as below array of json

[
  { labels: "city A", data: "Abc" },
  { labels: "city B", data: "Bcd" },
];


Solution 1:[1]

You can achieve it with something like this:

const original = {
  labels: ['city A', 'city B'],
  data: ['Abc', 'Bcd']
};

let merged = [];
const keys = Object.keys(original);
for (let idx = 0; idx < keys.length; idx++) {
  const entry = Object.assign.apply({},
    keys.map((key) => ({
      [key]: original[key][idx]
    }))
  );
  merged.push(entry);
}

console.log(merged);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1