Home > Back-end >  How to create an array from an array of objects by same key
How to create an array from an array of objects by same key

Time:01-24

I have an array of objects.
I want to merge the objects into a single array by same key. At that time, I also want to include other value in the array together.
It doesn't matter whether the merged array is an array or an object.

Current array:

[
  {
    "datetime": "2022-01-10",
    "a": 0.5,
    "b": 80.6,
    "c": 1002.2
  },
  {
    "datetime": "2022-01-11",
    "a": 0.7,
    "b": 80.4,
    "c": 1002.4
  },
  {
    "datetime": "2022-01-12",
    "a": 0.4,
    "b": 80.2,
    "c": 1002.3
  }
]

Expected result:

[
  [
    ["2022-01-10", 0.5], ["2022-01-11", 0.7], ["2022-01-12", 0.4]
  ],
  [
    ["2022-01-10", 80.6], ["2022-01-11", 80.4], ["2022-01-12", 1002.4]
  ],
  [
    ["2022-01-10", 1002.2], ["2022-01-11", 1002.4], ["2022-01-12", 1002.3]
  ]
]

or

{
  "a": [
    ["2022-01-10", 0.5], ["2022-01-11", 0.7], ["2022-01-12", 0.4]
  ],
  "b": [
    ["2022-01-10", 80.6], ["2022-01-11", 80.4], ["2022-01-12", 1002.4]
  ],
  "c": [
    ["2022-01-10", 1002.2], ["2022-01-11", 1002.4], ["2022-01-12", 1002.3]
  ]
}

I use forEach() and it works.
But I want to know if there are other ways.

const foo = [[], [], []];
json.forEach((item) => {
  const [a, b, c] = foo;
  a.push([item.datetime, item.a]);
  b.push([item.datetime, item.b]);
  c.push([item.datetime, item.c]);
});

CodePudding user response:

See this it uses map and then returns an array just like your ist

let res = data.map((m ,_) => [[m.datetime,m.a] , [m.datetime,m.b], [m.datetime,m.c]])

Or 2nd method because of comments

const res = [["a","b","c"].map(key => data.map(obj => [obj.datetime, obj[key]]))];

To see this Use

document.write(JSON.stringify(res , null , 4).replaceAll("" , "&nbsp;").replaceAll("\n","<br>"))

CodePudding user response:

you can use javascript reduce to optimize this:

const data = temp.reduce((acc,{a,b,c,datetime})=>{
  const [first,second,third] = acc;
  first.push([datetime, a]);
  second.push([datetime, b]);
  third.push([datetime, c]);
  return acc;
},[[],[],[]])

CodePudding user response:

You can use reduce function where the initial value will be an empty object. after that, you can check if that object contains that particular key or not and push the data accordingly

a = [
  {
    datetime: "2022-01-10",
    a: 0.5,
    b: 80.6,
    c: 1002.2,
  },
  {
    datetime: "2022-01-11",
    a: 0.7,
    b: 80.4,
    c: 1002.4,
  },
  {
    datetime: "2022-01-12",
    a: 0.4,
    b: 80.2,
    c: 1002.3,
  },
];

const solution = (key) => {
  return a.reduce((a, next) => {
    const valueToReturn = { ...a };
    const obj = JSON.parse(JSON.stringify(next));
    delete obj[key];
    Object.keys(obj).forEach((item) => {
      if (valueToReturn[item]) {
        valueToReturn[item].push([next[key], obj[item]]);
      } else {
        valueToReturn[item] = [];
        valueToReturn[item].push([next[key], obj[item]]);
      }
    });
    return valueToReturn;

   
  }, {});
};

console.log(solution("datetime"));

  • Related