简体   繁体   中英

turn json obj to array

I have this array of objects

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

I'm trying to get this format as an end result: [["thisA","thisC"], ["thatA","thatC"]]

I'm trying with a for loop

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

but I end up having ["thisA","thisC","thatA","thatC"]

You can do this with map() method.

 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) 

You coulöd push an array with the values. Beside that, you need to initialize i with zero.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM