[英]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
创建具有name
, status
和originalLanguage
字段以及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.