我有这个对象数组
[{
"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"]
答案 0 :(得分:4)
您可以使用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)
答案 1 :(得分:2)
你可以用数值推送一个数组。除此之外,您需要用零初始化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);