修改值
法一
let obj = {
a : '1',
b : '2',
c : '3'
}
Object.keys(obj).forEach(key=>{obj[key]=''})
console.log(obj)
法二 参数分别是对象 键key 值value
this.$set(this.dynamicForm,headMatchArr[i],'')
修改键
法一 改普通对象的键
var array = [
{id: 1,name: "小明"},
{id: 2,name: "小红"},
//旧key到新key的映射
var keyMap = {id: "value",name: "label"};
for (var i = 0; i < array.length; i++) {
var obj = array[i];
for (var key in obj) {
var newKey = keyMap[key];
if (newKey) {
obj[newKey] = obj[key];
delete obj[key];
console.log(array);
法二 es6:(改树的键)
this.categoryOption = res.data.map(item => {
return {
value: item.id,
label: item.name,
children: item.sonList.map(sonItem => {
return {
value: sonItem.id,
label: sonItem.name
console.log(this.categoryOption);
let arr = [
{ evaluateResult: "A类", evaluateTotal: 22, a: 1 },
{ evaluateResult: "B类", evaluateTotal: 22, b: 2 },
let keyMap = { evaluateResult: "name", evaluateTotal: "value" };
arr.map((item) => { //循环数组
var objs = Object.keys(item).reduce((newData, key) => { //获取键作为数组 第一个参数newData是初始值,为{}, 旧key赋值给新newKey
let newKey = keyMap[key] || key;
newData[newKey] = item[key]; //旧值赋值给新key的值
return newData;
}, {});
console.log(objs);
//{name: "A类", value: 22, a: 1}
//{name: "B类", value: 22, b: 2}
});
//返回数组形式
let arr = [
{ evaluateResult: "A类", evaluateTotal: 22, a: 1 },
{ evaluateResult: "B类", evaluateTotal: 22, b: 2 },
];
let newArr = []
let keyMap = { evaluateResult: "name", evaluateTotal: "value" };
arr.map((item) => {
var objs = Object.keys(item).reduce((newData, key) => {
let newKey = keyMap[key] || key;
newData[newKey] = item[key];
return newData;
}, {});
console.log(objs);
newArr.push(objs)
/*输出结果*/
//{name: "A类", value: 22, a: 1}
//{name: "B类", value: 22, b: 2}
});
console.log(newArr)
/* 结果: (2) [{…}, {…}]
0: {name: "A类", value: 22, a: 1}
1: {name: "B类", value: 22, b: 2}
length: 2*/
方法四:递归 处理后端返回的数据
//后端返回数据,我想把lable,sonLable换成title,sonList换成children
var obj = {
code: "00",
message: "获取成功",
data: [
id: 1,
lable: "老师",
sonList: [
sonId: 11,
sonLable: "老师初级",
sonId: 12,
sonLable: "老师中级",
sonId: 13,
sonLable: "老师高级",
id: 2,
lable: "学生",
sonList: [
sonId: 1,
sonLable: "学生初级一级",
sonId: 2,
sonLable: "学生初级二级",
sonId: 3,
sonLable: "学生初级三级",
id: 3,
lable: "考官",
sonList: [
sonId: 14,
sonLable: "考官初级",
sonId: 15,
sonLable: "考官中级",
sonId: 16,
sonLable: "考官高级",
//处理数据
function fn(arr) {
var ary = [];
arr.forEach((item) => {
var obj = {};
for (var k in item) {
if (k == "lable" || k == "sonLable") {
obj.title = item[k];
} else if (k == "sonList") {
// obj.children = [...item[k]];
obj.children = fn(item[k]);
} else {
obj[k] = item[k];
ary.push(obj);
return ary;
var newObj = fn(obj.data);
console.log(newObj);
又例如:把如下data1的key名称修改成data2中的key名称
data1: [ { appName: '应用1', capacity: 233456 }, { title: '应用2', key: 124535 }]
data2: [ { name: '应用1', value: 233456 }, { name: '应用2', value: 124535 } ]
//方法
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);
console.log(newArr)
return newArr;
let data2 = convertKey(data1, ['name', 'value']);