为了账号安全,请及时绑定邮箱和手机立即绑定

JS分配方法

JS分配方法

ITMISS 2021-04-11 10:09:15
有一个函数可以将密钥“ certificate”更改为“ certificate_car”。有没有一种方法可以通过直接使用assign方法更改数组来改善功能?是否可以?const arr = [{    "name": "BMW",    "price": "55 000",    "country": "Germany",    "certificate": "yes"  },  {    "name": "Mercedes-benz",    "price": "63 000",    "country": "Germany",    "certificate": "yes"  },  {    "name": "Mitsubishi",    "price": "93 000",    "constructor": "Bar John",    "door": "3",    "country": "Japan",  },  {    "name": "TOYOTA",    "price": "48 000",    "max_people": "7",    "country": "Japan",    "certificate": "yes"  },  {    "name": "Volkswagen",    "price": "36 000",    "constructor": "Pier Sun",    "country": "Germany",    "certificate": "no"  },];function certificateCar(arr) {  return arr.map(function(item) {    let itemCar = {};    let newItem = Object.assign({}, item);    Object.keys(newItem).forEach(item => {      itemCar[item.replace("certificate", "certificate_car")] = newItem[item]    });    return itemCar;  })}console.log(certificateCar(arr))
查看完整描述

2 回答

?
慕斯王

TA贡献1864条经验 获得超2个赞

您可以使用解构并更改所需的密钥名称,并保持其余名称不变


const arr = [{"name":"BMW","price":"55 000","country":"Germany","certificate":"yes"},{"name":"Mercedes-benz","price":"63 000","country":"Germany","certificate":"yes"},{"name":"Mitsubishi","price":"93 000","constructor":"Bar John","door":"3","country":"Japan",},{"name":"TOYOTA","price":"48 000", "max_people":"7","country":"Japan","certificate":"yes"},{"name":"Volkswagen","price":"36 000", "constructor":"Pier Sun","country":"Germany","certificate":"no"}, ];


const certificateCar = (arr) =>

   arr.map(({certificate,...rest}) =>({...rest,certificate_car:certificate}))


console.log(certificateCar(arr))


查看完整回答
反对 回复 2021-04-29
?
一只甜甜圈

TA贡献1836条经验 获得超5个赞


    const arr = [{

        "name": "BMW",

        "price": "55 000",

        "country": "Germany",

        "certificate": "yes"

      },

      {

        "name": "Mercedes-benz",

        "price": "63 000",

        "country": "Germany",

        "certificate": "yes"

      },

      {

        "name": "Mitsubishi",

        "price": "93 000",

        "constructor": "Bar John",

        "door": "3",

        "country": "Japan",

      },

      {

        "name": "TOYOTA",

        "price": "48 000",

        "max_people": "7",

        "country": "Japan",

        "certificate": "yes"

      },

      {

        "name": "Volkswagen",

        "price": "36 000",

        "constructor": "Pier Sun",

        "country": "Germany",

        "certificate": "no"

      },

    ];


    function certificateCar(arr) {


      return arr.map(function(item) {

        let newItem = {}

        delete Object.assign(newItem, item, {["certificate_car"]: item["certificate"] })["certificate"];

        return newItem

      })

      

    }

    console.log(certificateCar(arr)) 

如果您想使用Assign方法做到这一点,那将比替换键名更好


查看完整回答
反对 回复 2021-04-29
  • 2 回答
  • 0 关注
  • 181 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信