【js】Object.entries的用法

2023-11-02

Object.entries是返回一个键值对数组

const obj = {
	one: 1,
	two: 2,
	three: 3
}
const result = Object.entries(obj)
console.log(result)		// [[one, 1], [two, 2], [three, 3]]
const arr = ['one', 'two', 'three']
const result = Object.entries(arr)
console.log(result)		// [[0, 'one'], [1, 'two'], [2, 'three']]

如果想把两个数组中的数据一一对应合成一个,可以用Object.entries

const arr1 = ['大哈', '二哈', '三哈']
const arr2 = [15, 16, 17]
const result = Object.entries(arr2)?.map([key, value]: any => {
	return {
		name: arr1[key],
		age: value
	}
})
console.log(result)		
// [
	// {name: '大哈', age: 15},
	// {name: '二哈', age: 16},
	// {name: '三哈', age: 17}
// ]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【js】Object.entries的用法 的相关文章