0

I need a nested array, but I've got a array with nested objects.

What i have:

[
    { id: 1, title: 'title', value: 'test data'}
    { id: 2, title: 'title', value: 'test data'}
]

But i need:

[
    [ 1, 'title', 'test data']
    [ 2, 'title', 'test data']
]
sweebee
  • 59
  • 1
  • 6
  • 3
    Please fix the syntax errors. Put the quotes at the right places. – anshulk Apr 21 '17 at 09:14
  • What did you try to accomplish this? Did you use `for` statement or what? – Alex Slipknot Apr 21 '17 at 09:15
  • Possible duplicate of [Converting a JS object to an array](http://stackoverflow.com/questions/6857468/converting-a-js-object-to-an-array) – Peter Kota Apr 21 '17 at 09:17
  • Please take the [tour](http://stackoverflow.com/tour), have a look around, and read through the [help center](http://stackoverflow.com/help), in particular [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) Make an effort to solve the problem. If you run into a specific issue doing so, post a question with your attempt (including all the relevant code), saying what isn't working, and explaining your research so far. – Rory McCrossan Apr 21 '17 at 09:18

5 Answers5

6

You can perform a .map(x => Object.values(x)) on it.

Live example:

let src = [
    { id: 1, title: 'title', value: 'test data'},
    { id: 2, title: 'title', value: 'test data'}
];

let result = src.map(x => Object.values(x));

console.log(result);
Winestone
  • 1,500
  • 9
  • 19
3

You could use Object.values direct as callback.

var data = [{ id: 1, title: 'title', value: 'test data' }, { id: 2, title: 'title', value: 'test data'}],
    result = data.map(Object.values);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

using forEach loop:

var obj = [
    { id: 1, title: 'title', value: 'test data'},
    { id: 2, title: 'title', value: 'test data'}
];

var arr = [];

obj.forEach(function(o, k) {
 arr.push([o.id, o.title, o.value]);
});

console.log(arr);
Sojtin
  • 2,714
  • 2
  • 18
  • 32
0

you could try like this using Array#map and object.values().and your code have some syntax error missing 'and ,

var a = [{  id: 1,  title: 'title',  value: 'test data',}, {  id: 2,  title: 'title',  value: 'testdata',}];
var n = a.map(a=>Object.values(a))
console.log(n)
prasanth
  • 22,145
  • 4
  • 29
  • 53
  • I think we have just a little different view about 'readable code'. Here is your code that might look like https://jsfiddle.net/w4d8yw35/ Try to avoid inline code. – Alex Slipknot Apr 21 '17 at 10:02
0

You just need to use Array.proptotype.map() method along with Object.values() to loop your array and return the wanted result:

var results = data.map(function(item) {
  var elem = [];
  elem.push(Object.values(item));
  return elem;
});

Demo:

var data = [{
    id: 1,
    title: 'title',
    value: 'test data'
  },
  {
    id: 2,
    title: 'title',
    value: 'test data'
  }
];

var results = data.map(function(item) {
  var elem = [];
  elem.push(Object.values(item));
  return elem;
});

console.log(JSON.stringify(results));
cнŝdk
  • 31,391
  • 7
  • 56
  • 78