-1

How to do it dynamically if we have more objects in input data?

I have dataset array with multiple objects inside it, and objects can be any number. and I have to convert this data into newDataset object with dynamic array inside of it, this array of number dependence on dataset array's objects. I have done this if we have known number of dataset array's objects. for example: see below.

// Input data have dynamic more objects.
var dataset = [
  {
    date: "1 Jan 2020",
    data1: 44,
    data2: 40,
  },
  {
    date: "2 Jan 2020",
    data1: 37,
    data2: 60,
  },
  {
    date: "3 Jan 2020",
    data1: 75,
    data2: 90,
  },
];

var newDataset = {};

// // Output data should be in this form.
// var newDataset = {
//   date: ["1 Jan 2020", "2 Jan 2020", "3 Jan 2020"],
//   data1: [44, 37, 75],
//   data2: [40, 60, 90],
// };


// write your code below

var date = [];
var data1 = [];
var data2 = [];

for (var i = 0; i < dataset.length; i++) {
  date.push(dataset[i].date);
  data1.push(dataset[i].data1);
  data2.push(dataset[i].data2);
}

newDataset.date = date;
newDataset.data1 = data1;
newDataset.data2 = data2;
console.log(newDataset);

//end of your code

3 Answers 3

1

You could use a Map object

var dataset = [
  { date: "1 Jan 2020", data1: 44, data2: 40 },
  { date: "2 Jan 2020", data1: 37, data2: 60 },
  { date: "3 Jan 2020", data1: 75, data2: 90 }
];

const myMap = new Map();

dataset.forEach(obj => {
  Object.entries(obj).forEach(([k, v]) => {
    myMap.has(k) ? myMap.get(k).push(v) : myMap.set(k, [v]);
  })
});

console.log(Object.fromEntries(myMap.entries()));
.as-console-wrapper{min-height:100% !important; top: 0; }

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

1 Comment

This answer is much easier for beginners.
1

Although, you have already taken an answer as accepted, here I've a bit more abstract solution for you. Let's say, what if the objects in dataset are not strictly structured?, meaning some of these objects may have properties that you don't want in your newDataset. This following solution gives you power to filter the properties as you need.

const dataSet = [
    { date: "1 Jan 2020", data1: 44, data2: 40 },
    { date: "2 Jan 2020", data1: 37, data2: 60 },
    { date: "3 Jan 2020", data1: 75, data2: 90, unnecessary: 939393 },
  ];
const newDataset = {date: [], data1: [], data2: []} // if you need only these 3 properties.
const createNewDataset = (givenDataset) =>
    givenDataset.reduce(
      (builOn, current) => (
        Object.entries(current).forEach(
          ([key, value]) => (Array.isArray(builOn[key]) ? builOn[key].push(value): null)
        ),
        builOn
      ),
      newDataset
    );

console.log(createNewDataset(dataSet));

2 Comments

Remove unnecessary item is work fine, but it is not dynamic for more data item.
You meant more properties in dataset's object? Actually there should have no silver bullet for this problem's solution.
1

Following seems to work dynamically:

const src = [{date:"1 Jan 2020",data1:44,data2:40,},{date:"2 Jan 2020",data1:37,data2:60,},{date:"3 Jan 2020",data1:75,data2:90,},],

    rebuild = arr =>
      arr.reduce((r, o) => (
        Object.keys(o).forEach(key => (
          !r[key] && (r[key] = []), 
          r[key].push(o[key])
        )), 
        r
      ), {})
          
console.log(rebuild(src))
.as-console-wrapper{min-height:100%;}

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.