将 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)

你可以用值推送一个数组。除此之外,您需要用零初始化i

var objects = [{ A: "thisA", B: "thisB", C: "thisC" }, { A: "thatA", B: "thatB", C: "thatC" }],
    array = [],
    i;
for (i = 0; i < objects.length; i++) {
    array.push([objects[i].A, objects[i].C]);
}
console.log(array);

最新更新