Attach an array of objects by javascript property

I have array of objects:

var x = [{a: 1,b:2}, {a:3,b:4}, {a: 5,b:6}];

I need to combine an array as follows:

1,2 
3,4
5,6

I do not want to use from lodashorunderscore

How can I get a connection array of objects?

+4
source share
3 answers
const x = [{a: 1,b:2}, {a:3,b:4}, {a: 5,b:6}];    
console.log(x.map(Object.values));

Conclusion:

[
  [1,2],
  [3,4],
  [5,6]
]

Also, if you really need a line (incomprehensible from your question)

x
  .map(o => Object.values(o).join(','))
  .join('\n')
+6
source

A simple card will do it!

let xs = [{a: 1,b:2}, {a:3,b:4}, {a: 5,b:6}]
let out = xs.map(({a,b})=> [a,b])
console.log(out)
//=> [ [1,2], [3,4], [5,6] ]
Run codeHide result

Here's a pre-ES6 answer to help

var xs = [{a: 1,b:2}, {a:3,b:4}, {a: 5,b:6}]
var out = xs.map(function(x) { return [x.a, x.b] })
console.log(out)
//=> [ [1,2], [3,4], [5,6] ]
Run codeHide result
+4
source

I am using this code:

x.map(Object.values).join("\n")

+1
source

Source: https://habr.com/ru/post/1649987/


All Articles