<script>
1.目前数组
var aa=[
{num:310,numValue1:'啦啦啦'},
{cum:320,numValue2:'嘻嘻嘻'},
{vum:330,numValue3:'哈哈哈'},
{sum:340,numValue:'吱吱吱'}
];
1.目标数组
[
{value:310,name:'啦啦啦'},
{value:320,name:'嘻嘻嘻'},
{value:330,name:'哈哈哈'},
{value:340,name:'吱吱吱'}
];
function convertKey(arr,key){
let newArr = [];
arr.forEach((item, index) => {
let newObj = {};
for (var i = 0; i < key.length; i++) {
newObj[key[i]] = item[Object.keys(item)[i]]
}
newArr.push(newObj);
})
return newArr;
}
let data2 = convertKey(aa, ['name', 'value']);
console.log(data2)
</script>