繁体   English   中英

如何使用另一个数组中其他对象的键替换对象数组中的值

[英]How to replace values in my array of objects using key from other object in another array

我有2个对象数组

注意:状态和原始语言不能随时手动设置,因为它们一直在变化,这些是自定义字段。 来自字段的段构成所有自定义字段。

const items = [
  {
    name: 'Surviving the Game',
    status: 'ffdb29ba075fcbc0b71295c31a13d64f',
    original-language: 'b4ebbe06702794d1cf375274197267b2',
  },
  {
    name: 'Some Movie',
    status: 'cd53c082c6ca9e7d3ec66890e66c01f3',
    original-language: '7a1cac74217747933bb3915888dea090',
  },
];
const fields = [
  {
    slug: 'status',
    options: [
      {
        name: 'Released',
        id: 'ffdb29ba075fcbc0b71295c31a13d64f',
      },
      {
        name: 'Upcoming',
        id: 'cd53c082c6ca9e7d3ec66890e66c01f3',
      },
    ],
  },
  {
    slug: 'original-language',
    options: [
      {
        name: 'de',
        id: 'b4ebbe06702794d1cf375274197267b2',
      },
      {
        name: 'en',
        id: '7a1cac74217747933bb3915888dea090',
      },
    ],
  },
];

[项目]中的status和原始语言具有与相应字段数组中的选项匹配的id值。

我正在尝试为[项目]返回一个新数组,其名称来自具有匹配ID的选项。

例如:

[
  {
    name: 'Surviving the Game',
    status: 'Released',
    original-language: 'de',
  },
  {
    name: 'Some Movie',
    status: 'Upcoming',
    original-language: 'en',
  },
];

我将如何使用ES6 / 7做到这一点?

我不确定从哪里开始

我将通过创建一个lookup对象来实现此目的,该对象包含您的状态和语言的查找。 然后,在通过items映射时可以使用此查找对象。

 var items = [ { name: 'Surviving the Game', status: 'ffdb29ba075fcbc0b71295c31a13d64f', "original-language": 'b4ebbe06702794d1cf375274197267b2' }, { name: 'Some Movie', status: 'cd53c082c6ca9e7d3ec66890e66c01f3', "original-language": '7a1cac74217747933bb3915888dea090' } ]; var fields = [ { slug: 'status', options: [ { name: 'Released', id: 'ffdb29ba075fcbc0b71295c31a13d64f' }, { name: 'Upcoming', id: 'cd53c082c6ca9e7d3ec66890e66c01f3' } ] }, { slug: 'original-language', options: [ { name: 'de', id: 'b4ebbe06702794d1cf375274197267b2' }, { name: 'en', id: '7a1cac74217747933bb3915888dea090' } ] } ]; const lookup = {}; fields.forEach(field => { lookup[field.slug] = field.options.reduce((all, option) => ({ ...all, [option.id]: option.name }), {}) }); const translatedItems = items.map(item => { return Object.entries(item) .reduce((all, [key, val]) => ({ ...all, [key]: lookup[key] ? lookup[key][val] : val }),{}); }); console.log(translatedItems); 

我将定义一个获取字段值的函数,如下所示:

function valueForField(field, id) {
    const field = fields.find((itemfields) => itemfields.slug === field);
    if(!field) return null;
    const option = field.options.find(option => option.id === id);
    if(!option) return null;
    return option.name;
}

然后可以这样使用:

const newItems = items.map(item => {
    const { name } = item;
    const newItem = {name};
    newItem["original-language"] = valueForField('original-language', item["original-language"]);
    newItem.status = valueForField('status', item.status);
    return newItem;
});

使用map创建具有namestatusoriginalLanguage字段以及find方法的新对象数组, name从每个status标识符的fields获取name

 const items = [{ name: 'Surviving the Game', status: 'ffdb29ba075fcbc0b71295c31a13d64f', originalLanguage: 'b4ebbe06702794d1cf375274197267b2', }, { name: 'Some Movie', status: 'cd53c082c6ca9e7d3ec66890e66c01f3', originalLanguage: '7a1cac74217747933bb3915888dea090', }, ]; const fields = [{ slug: 'status', options: [{ name: 'Released', id: 'ffdb29ba075fcbc0b71295c31a13d64f', }, { name: 'Upcoming', id: 'cd53c082c6ca9e7d3ec66890e66c01f3', }, ], }, { slug: 'original-language', options: [{ name: 'de', id: 'b4ebbe06702794d1cf375274197267b2', }, { name: 'en', id: '7a1cac74217747933bb3915888dea090', }, ], }, ], newArr = items.map(i => ({ name: i.name, status: fields.find(f => f.slug == 'status').options.find(o => o.id == i.status).name, originalLanguage: fields.find(f => f.slug == 'original-language').options.find(l => l.id == i.originalLanguage).name })); console.log(newArr); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM