例如fileDate = [ {deleteValue: 0, downloadValue: 0}, {deleteValue: 1, downloadValue: 1}, {deleteValue: 2, downloadValue: 0}, ] 数组中的多个对象存在共同的属性deleteValue和downloadValue,我们在项目中有时候需要提取出deleteValue,
将所有的deleteValue属性值收集起来做处理,我们可以使用两次循环来完成它
let newObj = {} const fileDate = [ {deleteValue: 0, downloadValue: 0}, {deleteValue: 1, downloadValue: 1}, {deleteValue: 2, downloadValue: 0}, ] const keys = Object.keys(fileDate[0]) keys.forEach((item,index) => { console.log(keys[index]); const value = keys[index] const arr = fileDate.map(item => item[value]) newObj[value] = arr }) console.log('newObj',newObj);
//newObj: {deleteValue: [0, 1, 2], downloadValue: [0, 1, 0] }
标签:提取,数组,对象,newObj,keys,deleteValue,downloadValue,const,fileDate From: https://www.cnblogs.com/ww-garden/p/16743878.html