简体   繁体   English

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

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

I have 2 arrays of objects 我有2个对象数组

NOTE: status and original-language can't be set manually as they change all the time, these are custom fields. 注意:状态和原始语言不能随时手动设置,因为它们一直在变化,这些是自定义字段。 The slugs from fields make up all custom fields. 来自字段的段构成所有自定义字段。

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 and original-language in [items] have an id value which matches an option in the corresponding fields array. [项目]中的status和原始语言具有与相应字段数组中的选项匹配的id值。

I am trying to return a new array for [items] with the name from options with the matching id. 我正在尝试为[项目]返回一个新数组,其名称来自具有匹配ID的选项。

eg: 例如:

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

How would I go about this with ES6/7? 我将如何使用ES6 / 7做到这一点?

I am not sure where to start 我不确定从哪里开始

I would accomplish this by creating a lookup object that houses lookups for both your statuses and languages. 我将通过创建一个lookup对象来实现此目的,该对象包含您的状态和语言的查找。 You can then use this lookup object when mapping through your items . 然后,在通过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); 

I'd define a function that obtains the value for a field, like so: 我将定义一个获取字段值的函数,如下所示:

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;
}

This can then be used like so: 然后可以这样使用:

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;
});

Use map to create a new array of objects having name , status and originalLanguage fields along with the find method to get the name from fields for every status identifier. 使用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.

相关问题 将数组转为对象,并从另一个 object 添加键值 - Turn array into objects, and add key values from another object 用另一个数组/对象javascript的键值替换数组/对象的键值 - Replace array/objects key value with key value from another array/object javascript 对于每个 object,如何将特定键值从一个 object 添加到另一个对象数组 - How to add specific key values from one object to another array of objects, for each object 从 JavaScript 中的另一个对象数组替换一个对象数组的值 - Replace values of an array of objects from another array of objects in JavaScript 如何从具有值作为另一个对象数组的 object 中删除键值对? - How to remove key value pair from an object having values as another array of objects? 如何使用另一个数组按键/值从数组中提取对象 - How extract object by key/value from array using an other array 从一个 Array 对象中提取键值,并使用带下划线 javascript 的提取值从其他对象中过滤 - Extract key values from one Array object and filter from other objects with the extracted values with underscore javascript 使用来自另一个对象的值创建一个对象数组 - Create an array of objects with values from another object 从另一个对象数组向一个对象添加值 - Add values to an object from another array of objects 将 object 数组中的值替换为 JavaScript 中另一个数组中的值 - Replace values in array of object with values from another array in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM