js对象数组转换之Object.fromEntries和Object.entries

1.Object.entries()方法可以把对象转换成二维数组

let cars = {'audi':1,'bmw':2,'ubaru':3,'toyota':4}
let arrayCars = Object.entries(cars)
console.log(arrayCars)//[ [ 'audi', 1 ], [ 'bmw', 2 ], [ 'ubaru', 3 ], [ 'toyota', 4 ] ]

2.Object.fromEntries()方法可以把二维数组转换成对象,与1方法互逆操作

let arrayCars = [['audi','aaa'],['bmw','bbb'],['ubaru','ccc'],['toyota','ddd']]
let objectCars = Object.fromEntries(arrayCars)
console.log(objectCars)//{ audi: 'aaa', bmw: 'bbb', ubaru: 'ccc', toyota: 'ddd' }

你可能感兴趣的:(js对象数组转换之Object.fromEntries和Object.entries)