0

How can I easily convert this (array of dictionaries):

[ { a: 1,
    b: 1,
    c: 'something',
    d: 1 },
  { a: 23443,
    b: 2111,
    c: 'something 2',
    d: 1456 }
]

to this (array of arrays):

[ [ 1,
    1,
    'something',
    1 ],
  [ 23443,
    2111,
    'something2',
    1456 ]
]

1 Answer 1

3

Use Array.prototype.map and for-in loop

The map() method creates a new array with the results of calling a provided function on every element in this array

The for...in statement iterates over the enumerable properties of an object

Try this:

var input = [{
  a: 1,
  b: 1,
  c: 'something',
  d: 1
}, {
  a: 23443,
  b: 2111,
  c: 'something2',
  d: 1456
}];
var op = input.map(function(inp) {
  var arr = [];
  for (var i in inp) {
    arr.push(inp[i]);
  }
  return arr;
});
console.log(op);

OR use Object.keys(YOUR_OBJECT):

var input = [{
  a: 1,
  b: 1,
  c: 'something',
  d: 1
}, {
  a: 23443,
  b: 2111,
  c: 'something2',
  d: 1456
}];
var op = input.map(function(inp) {
  return Object.keys(inp).map(function(key) {
    return inp[key];
  })
});
console.log(op);

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

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.