1

I want transform an array of objects into 2D array (array of arrays). I try with map and Object.keys(), but I want push the first value of each objects into the first array, the second value of each objects into the second array, etc ... Thank you for your answers.

The good result

[
  ['2', '5', '8', '10', '12'],
  ['4', '10', '16', '20', '24'],
  ['6', '15', '24', '30', '36'],
  ['10', '25', '40', '50', '60']
]

The actual result

[
  ['2', '4', '6', '10'],
  ['5', '10', '15', '25'],
  ['8', '16', '24', '40'],
  ['10', '20', '30', '50'],
  ['12', '24', '36', '60']
]

let obj = [
  {
    line1: '2',
    line2: '4',
    line3: '6',
    line4: '10'
  }, {
    line1: '5',
    line2: '10',
    line3: '15',
    line4: '25'
  }, {
    line1: '8',
    line2: '16',
    line3: '24',
    line4: '40'
  }, {
    line1: '10',
    line2: '20',
    line3: '30',
    line4: '50'
  }, {
    line1: '12',
    line2: '24',
    line3: '36',
    line4: '60'
  }
];

var output = obj.map(function(obj) {
  return Object.keys(obj).map(function(key) { 
    return obj[key];
  });
});

console.log(output);

1

2 Answers 2

3

You need the indices for transfoming and take the reversed indices for accessing the result set.

var array = [{ line1: '2', line2: '4', line3: '6', line4: '10' }, { line1: '5', line2: '10', line3: '15', line4: '25' }, { line1: '8', line2: '16', line3: '24', line4: '40' }, { line1: '10', line2: '20', line3: '30', line4: '50' }, { line1: '12', line2: '24', line3: '36', line4: '60' }],
    result = array.reduce((r, o, i) => {
        Object.keys(o).forEach((k, j) => (r[j] = r[j] || [])[i] = o[k]);
        return r;
    }, []);

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

For a stable result, you could take the keys as array in advance.

var array = [{ line1: '2', line2: '4', line3: '6', line4: '10' }, { line1: '5', line2: '10', line3: '15', line4: '25' }, { line1: '8', line2: '16', line3: '24', line4: '40' }, { line1: '10', line2: '20', line3: '30', line4: '50' }, { line1: '12', line2: '24', line3: '36', line4: '60' }],
    keys = ['line1', 'line2', 'line3', 'line4'],
    result = array.reduce((r, o, i) => {
        keys.forEach((k, j) => (r[j] = r[j] || [])[i] = o[k]);
        return r;
    }, []);

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

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

Comments

1

You can also use following approach.

let obj = [
  {
    line1: '2',
    line2: '4',
    line3: '6',
    line4: '10'
  }, {
    line1: '5',
    line2: '10',
    line3: '15',
    line4: '25'
  }, {
    line1: '8',
    line2: '16',
    line3: '24',
    line4: '40'
  }, {
    line1: '10',
    line2: '20',
    line3: '30',
    line4: '50'
  }, {
    line1: '12',
    line2: '24',
    line3: '36',
    line4: '60'
  }
];

var output = [];
for(var i = 0; i < obj.length; i++){
   var index = 0;
   for(var values in obj[i]){
      if(output[index] == undefined){
        output[index] = [];
      }
      output[index].push(obj[i][values]);
      index++;
   }
}

console.log(output);

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.