javascript - 将对象数组转换为二维数组
问题描述
我怎样才能转换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"]]
解决方案
你可以使用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 - Vanilla JavaScript Flip Card 游戏不工作
- c++ - C++ 类作为列表长度说明符?
- python - 从 Flask 调用任意 Python 函数的更优雅的方式
- mysql - 围绕经过时间优化 MySQL 查询
- nlp - 尝试训练 spacy ner 管道以添加新命名实体时出现意外类型的 NER 数据
- c# - 与 C# 控制台应用程序中的相同代码相比,Regex.Match 在 Unity 中返回不同/错误的结果
- c++ - 无法解释为什么 c++ 向量分配失败
- android - 如何修复 Android Studio 模拟器总是向上旋转?
- python - 谷歌自己的参考示例甚至不起作用?为 GCS 编写自定义签名 URL
- rabbitmq - 基于 AMQP 的微服务注册和发现