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']
]
4
  • 3
    Please fix the syntax errors. Put the quotes at the right places. Commented Apr 21, 2017 at 9:14
  • What did you try to accomplish this? Did you use for statement or what? Commented Apr 21, 2017 at 9:15
  • Possible duplicate of Converting a JS object to an array Commented Apr 21, 2017 at 9:17
  • Please take the tour, have a look around, and read through the help center, in particular How do I ask a good question? 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. Commented Apr 21, 2017 at 9:18

5 Answers 5

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);

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

1 Comment

You are filled with creativity as always. :)
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; }

Comments

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);

Comments

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)

1 Comment

I think we have just a little different view about 'readable code'. Here is your code that might look like jsfiddle.net/w4d8yw35 Try to avoid inline code.
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));

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.