使用JS中的reduce方法重命名对象中的键

3
以下是我想要用 value 替换 countryID ,用 label 替换 countryName 的对象。
在同一对象中,我有一个 localLanguages 数组,我正在尝试将 language 重命名为 label,将 languageCode 重命名为 value
数组 -
var obj = [{
    "countryID": "CON1010",
    "countryName": "Poland",
    "countryCode": "pl",
    "localLanguages": [{
        "language": "English",
        "languageCode": "en"
      },
      {
        "language": "Polish",
        "languageCode": "en"
      }
    ]
  },
  {
    "countryID": "CON1011",
    "countryName": "UK",
    "countryCode": "uk",
    "localLanguages": [{
      "language": "English",
      "languageCode": "en"
    }]
  }
];

转化为 -

var obj = [{
    "value": "CON1010",
    "label": "Poland",
    "countryCode": "pl",
    "localLanguages": [{
        "label": "English",
        "value": "en"
      },
      {
        "label": "Polish",
        "value": "en"
      }
    ]
  },
  {
    "value": "CON1011",
    "label": "UK",
    "countryCode": "uk",
    "localLanguages": [{
      "label": "English",
      "value": "en"
    }]
  }
];

代码 -

arr.map(x => {
  var newObj = Object.keys(x).reduce((obj, key) => {
    if (key !== 'countryID') {
      obj[key] = x[key]
    }

    if (key === 'countryID') {
      obj.value = x.countryID;
    }
  }, {})
  console.log(newObj);

  return newObj;
})
4个回答

4

以下是使用ES6解构和映射的解决方案:

 const arr = [{"countryID":"CON1010","countryName":"Poland","countryCode":"pl","localLanguages":[{"language":"English","languageCode":"en"},{"language":"Polish","languageCode":"en"}]},{"countryID":"CON1011","countryName":"UK","countryCode":"uk","localLanguages":[{"language":"English","languageCode":"en"}]}];

 const result = arr.map(item => {

  let localLanguages = item.localLanguages.map(i => {
    const { language: label, languageCode: value, ...rest } = i;
    return { label, value, ...rest };
  });

  const { countryID: value, countryName: label, ...rest } = item;

  return { value, label, ...rest, localLanguages };
});

console.log(result)

最好将代码放在一个函数中,然后最好能够解释每个步骤的含义,这样就清楚了代码需要进行哪些更改。另外一件事——当操作对象时,最好使用Object.assign({}, obj)来操作对象的副本。 - Rotem
@Rotem 我稍微修改了代码以提供更多的解释,现在你可以运行代码并查看结果。我只是从 arr 数组中进行了迭代,并根据上述问题的要求创建了一个新的数组。 - Mergim Bajrami

2
使用Array.map()来转换外部对象,并使用另一个map来转换localLanguages:

const arr = [{"countryID":"CON1010","countryName":"Poland","countryCode":"pl","localLanguages":[{"language":"English","languageCode":"en"},{"language":"Polish","languageCode":"en"}]},{"countryID":"CON1011","countryName":"UK","countryCode":"uk","localLanguages":[{"language":"English","languageCode":"en"}]}];

const result = arr.map(o => ({
  value: o.countryID,
  label: o.countryName,
  countryCode: o.countryCode,
  localLanguages: o.localLanguages.map(l => ({
    value: l.languageCode,
    label: l.language
  }))
}));

console.log(result)


0

你忘记在reduce函数中返回obj值了

 var newObj = Object.keys(x).reduce( (obj, key) => {
    if(key !== 'countryID') {
      obj[key] = x[key]
    }

    if(key === 'countryID') {
      obj.value = x.countryID;
    }
  }, {})

0

这是用于更改键的函数。将其与数组中的每个元素递归使用,但检查每个元素的类型。

function changeKeys(obj) {
  const rename = {
    'countryID': 'value',
    'countryName': 'label',
    'language': 'label',
    'languageCode': 'value'
  }
  return Object.keys(obj)
    .reduce(
    (acc, rec) => {
      if (typeof rename[rec] !== 'undefined') {
        return {...acc, [rename[rec]]: obj[rec]}
      }
      return {...acc, [rec]: obj[rec]}
    }, {}
  )
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接