首页 > 解决方案 > 将对象数组转换为二维数组

问题描述

我怎样才能转换Array这个Objects

var tags= [
  {id: 0, name: "tag1", project: "p1", bu: "test"},
  {id: 1, name: "tag2", project: "p1", bu: "test"},
  {id: 2, name: "tag3", project: "p3", bu: "test"}
];

进入这个二维array

[["tag1","p1", "test"],
["tag2","p1", "test"],
["tag3","p3", "test"]]

标签: javascriptnode.jsarraysobject

解决方案


你可以使用map

var tags= [ {id: 0, name: "tag1", project: "p1", bu: "test"}, {id: 1, name: "tag2", project: "p1", bu: "test"}, {id: 2, name: "tag3", project: "p3", bu: "test"} ];
var res=tags.map(o=>[o.name,o.project,o.bu])
console.log(res)

或者您可以使用更通用的方法

var tags= [ {id: 0, name: "tag1", project: "p1", bu: "test"}, {id: 1, name: "tag2", project: "p1", bu: "test"}, {id: 2, name: "tag3", project: "p3", bu: "test"} ];
var res = tags.map(({id,...rest}) => Object.values(rest))
console.log(res)


推荐阅读