将 json obj 转为数组

乔纳森

我有这个对象数组

[{
    "A": "thisA",
    "B": "thisB",
    "C": "thisC"
}, {
    "A": "thatA",
    "B": "thatB",
    "C": "thatC"
}]

我试图将这种格式作为最终结果: [["thisA","thisC"], ["thatA","thatC"]]

我正在尝试使用 for 循环

var arr = [],
    arr2 = [];
for (var = i; i < obj.length; i++) {
    arr.push(obj[i].A, obj[i].C);
    arr2.push(arr);
}

但我最终有 ["thisA","thisC","thatA","thatC"]

内纳德·弗拉卡尔

你可以用map()方法来做到这一点

const data = [{"A": "thisA","B": "thisB","C": "thisC"}, {"A": "thatA","B": "thatB","C": "thatC"}]

const result = data.map(({A, C}) => [A, C]);
console.log(result)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章