多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 一、第一种:将键和值分别组合成新数组; ``` let ageDate = { "20岁": "15.0", "40-50岁": "8.0", "30-40岁": "23.0" } let newageName = [] let newageNum = [] for (let a in ageDate ) { newageName.push(a); //获取到键的名称,放进新数组里 newageNum.push(ageDate [a]);//获取值的内容,放进新数组里 } console.log(newageName) //打印结果:["20岁", "40-50岁", "30-40岁"] ``` ## 二、第二种:将键和值重新组合成新的对象加入到新的数组里;for in ``` let ageDate = { "20岁": "15.0", "40-50岁": "8.0", "30-40岁": "23.0" } let newageName = [] for (let a in ageDate ) { let newObj = {}; newObj.name = a; // 新的键名,旧的键名变成值; newObj.value = ageDate[a]; //将值与新定义的键value配对; newageName.push(newObj); } console.log(newageName) ```