通过替换在另一个对象中找到的属性名称来创建新对象

因此,有很多问题涉及如何遍历对象并对属性名称进行简单更改,但我要解决的问题有点棘手,非常感谢一些帮助。


本质上,我想制作一个像这样的对象:


{

   home_number: '1234',

   customer: {

     name: {

       last_name: 'Smith',

     },

   },

};

变成这个


{

   home_number: '1234',

   individual: {

     name: {

       lastName: 'Smith',

     },

   },

};

目前,我的功能如下


function restructure(obj) {

  let newObj = {};

  const newKeys = {

    fed_tax_id: 'federalTaxId',

    company_structure: 'companyStructure',

    home_number: 'homeNumber',

    customer: 'individual',

    first_name: 'firstName',

    last_name: 'lastName',

  }

}

  for(let key in obj){

    if (typeof obj[key] === 'object') {

      restructure(obj[key])

    }

    if(Object.hasOwnProperty.call(newKeys, key)){

      let newProperty = newKeys[key]

      newObj[newProperty] = obj[key]

    }

    if(!Object.hasOwnProperty.call(newKeys, key)){

      newObj[key] = obj[key]

    }

return newObj;

  }

我目前正在努力解决的一件事是删除需要更改的键(我创建了 newKeys 对象作为显示需要更改的键以及它们需要更改的内容的一种方式)。即在这种情况下,当客户更改为个人时,它也会将“last_name”更改为“lastName”。


使用我目前的功能,对象返回如下:


{ homeNumber: '1234' }

谢谢 :) 如果已经问过这个问题,请告诉我,但是在搜索了整个地方之后,我找不到与此足够接近的问题。


人到中年有点甜
浏览 137回答 3
3回答

catspeake

由于restructure返回新对象,因此您需要分配递归调用的结果restructure,否则它将未被使用,这就是您当前代码中发生的情况。但是映射条目数组可能会更容易 -如果对象具有该键,则将条目中的键替换为对象上的关联值,然后将条目转回具有以下内容的对象Object.fromEntries:const newKeys = {  fed_tax_id: 'federalTaxId',  company_structure: 'companyStructure',  home_number: 'homeNumber',  customer: 'individual',  first_name: 'firstName',  last_name: 'lastName',};const restructure = obj => Object.fromEntries(  Object.entries(obj).map(    ([key, val]) => [      newKeys[key] || key,      typeof val === 'object' && val !== null ? restructure(val) : val    ]  ));console.log(restructure({   home_number: '1234',   customer: {     name: {       last_name: 'Smith',     },   },}));请记住typeof nullgive object,因此您需要null在递归重组之前进行检查(如上面的代码中所做的那样),否则您可能偶尔会遇到错误。

一只斗牛犬

您可以使用delete从对象中删除属性。注意:我的函数将修改原始对象。如果您想复制对象,请确保使用JSON方法进行深层复制const newKeys = {    fed_tax_id: 'federalTaxId',    company_structure: 'companyStructure',    home_number: 'homeNumber',    customer: 'individual',    first_name: 'firstName',    last_name: 'lastName',}const obj = {   home_number: '1234',   individual: {     name: {       lastName: 'Smith',     },   },};function restructure(obj){  for(let k in obj){    if(typeof obj[k] === "object" && !Array.isArray(obj[k])){      restructure(obj[k]);    }    if(newKeys[k]){      obj[newKeys[k]] = obj[k];      delete obj[k];    }  }}restructure(obj);console.log(obj)

qq_花开花谢_0

您可以使用Object.entriesand Object.fromEntries,另外,只创建一次 newKeys 对象,而不是在每次调用函数时创建它。const newKeys = {  fed_tax_id: 'federalTaxId',  company_structure: 'companyStructure',  home_number: 'homeNumber',  customer: 'individual',  first_name: 'firstName',  last_name: 'lastName',}function restructure(obj) {  let entries = Object.entries(obj).map(([key, val]) => {    val = typeof val === 'object' && !Array.isArray(val) ? restructure(val) : val    return [newKeys[key] || key, val]  });  return Object.fromEntries(entries)}console.log(restructure({  home_number: '1234',  customer: {    name: {      last_name: 'Smith',    },  },}))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript