javascript - 将对象数组转换为二维数组

标签 javascript node.js arrays object

如何转换 Objects

Array
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"}
];

进入这个二维数组:

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

最佳答案

你可以使用 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)

关于javascript - 将对象数组转换为二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63003485/

相关文章:

ios - 标签栏 Controller : Add/Remove from Array

javascript - 在 javascript 中的对象数组中搜索深度嵌套的值

javascript - 暂时禁用javascript中的所有事件

javascript - 从 csv 返回数组变量 - javascript/Node

javascript - 使用 node 和 browserify 制作 http 服务器

C++ node.js Addons - Init() 中的参数说明

javascript - 将 promise 项替换为数组中已实现的值

javascript - 是否可以修改已插入交叉过滤器的数据?

javascript - 对象不支持 IE9 中的属性或方法 'append'

javascript - 即 : Undocumented "cache" attribute defined for input elements?