js修改数组对象的Key和指定的值

1,修改数组对象的Key

 /**/
    var Arrydata = [{name:'Lily',age:18},{name:'BKqq',age:22}]
    //旧key到新key的映射
    var keyMap = {
      // id: "value",
      name: "label",
      age: "value"
    };

    for (var i = 0; i < Arrydata.length; i++) {
      var obj = Arrydata[i];
      for (var key in obj) {
        var newKey = keyMap[key];
        if (newKey) {
          obj[newKey] = obj[key];
          delete obj[key];
        }
      }
    }
    console.log(Arrydata,'修改后数组对象---')

js修改数组对象的Key和指定的值_第1张图片

2,修改数组对象中指定的值


var stooges = [
            {label:1,value:1,placeCode: 'sh', stock_code: 40},
            {label:1,value:1,placeCode: 'sh', stock_code: 50},
            {label:1,value:1,placeCode: 'sz', stock_code: 60,status:1},
            {label:1,value:1,placeCode: 'sz', stock_code: 39,status:1}
        ];
  for(var i=0;i<stooges.length;i++){
      stooges[i].placeCode === 'sh'?stooges[i].placeCode = '沪': stooges[i].placeCode = '深'
  }
  console.log(stooges,'值修改后数组对象---')

js修改数组对象的Key和指定的值_第2张图片

你可能感兴趣的:(Js,Html,Css)