(JavaScript)在不同的对象数组中找到相同的值
我正在React应用程序中制作课程卡组件。我有两个带有数据填充这些卡的数组。
对象的课程数组:
const courses = [
{
id: 'de5aaa59-90f5-4dbc-b8a9-aaf205c551ba',
title: 'JavaScript',
description: `Lorem Ipsum is simply dummy text of the printing and
typesetting industry. Lorem Ipsum
has been the industry's standard dummy text ever since the
1500s, when an unknown
printer took a galley of type and scrambled it to make a type
specimen book. It has survived
not only five centuries, but also the leap into electronic typesetting, remaining essentially u
nchanged.`,
creationDate: '8/3/2021',
duration: 160,
authors: [
'27cc3006-e93a-4748-8ca8-73d06aa93b6d',
'f762978b-61eb-4096-812b-ebde22838167',
],
},
{
id: 'b5630fdd-7bf7-4d39-b75a-2b5906fd0916',
title: 'Angular',
description: `Lorem Ipsum is simply dummy text of the printing and
typesetting industry. Lorem Ipsum
has been the industry's standard dummy text ever since the
1500s, when an unknown
printer took a galley of type and scrambled it to make a type
specimen book.`,
creationDate: '10/11/2020',
duration: 210,
authors: [
'df32994e-b23d-497c-9e4d-84e4dc02882f',
'095a1817-d45b-4ed7-9cf7-b2417bcbf748',
],
},
];
作者的对象数组:
const authors = [
{
id: '27cc3006-e93a-4748-8ca8-73d06aa93b6d',
name: 'Vasiliy Dobkin',
},
{
id: 'f762978b-61eb-4096-812b-ebde22838167',
name: 'Nicolas Kim',
},
{
id: 'df32994e-b23d-497c-9e4d-84e4dc02882f',
name: 'Anna Sidorenko',
},
{
id: '095a1817-d45b-4ed7-9cf7-b2417bcbf748',
name: 'Valentina Larina',
},
];
我的尝试匹配和“脱颖而出”,作者:
const COURSE = props.value;
// AUTHOR = authors.find((el) => el.id === COURSE.authors[0]);
// const filteredArray = array1.filter(value => array2.includes(value));
// arrA.filter(x => arrB.includes(x));
const authorsDeHash = (authorsHashed, authors) => {
// const myarr = [];
for (let i of authorsHashed) {
// console.log(authorsHashed);
console.log(authors.find((el) => el.id === authorsHashed[i]));
// myarr.push(authors.find((el) => el.id === authorsHashed[i]));
}
};
authorsDeHash(COURSE.authors, authors);
upd:既然我已经从父级组件中传递了一门课程,所以我无需搜索整个“课程”阵列中的值。因此,基本上,我只需要为一门课程找到作者的真实姓名。
I am making a courses card component in the React app. I have two arrays with the data to fill these cards.
THE COURSES ARRAY OF OBJECTS:
const courses = [
{
id: 'de5aaa59-90f5-4dbc-b8a9-aaf205c551ba',
title: 'JavaScript',
description: `Lorem Ipsum is simply dummy text of the printing and
typesetting industry. Lorem Ipsum
has been the industry's standard dummy text ever since the
1500s, when an unknown
printer took a galley of type and scrambled it to make a type
specimen book. It has survived
not only five centuries, but also the leap into electronic typesetting, remaining essentially u
nchanged.`,
creationDate: '8/3/2021',
duration: 160,
authors: [
'27cc3006-e93a-4748-8ca8-73d06aa93b6d',
'f762978b-61eb-4096-812b-ebde22838167',
],
},
{
id: 'b5630fdd-7bf7-4d39-b75a-2b5906fd0916',
title: 'Angular',
description: `Lorem Ipsum is simply dummy text of the printing and
typesetting industry. Lorem Ipsum
has been the industry's standard dummy text ever since the
1500s, when an unknown
printer took a galley of type and scrambled it to make a type
specimen book.`,
creationDate: '10/11/2020',
duration: 210,
authors: [
'df32994e-b23d-497c-9e4d-84e4dc02882f',
'095a1817-d45b-4ed7-9cf7-b2417bcbf748',
],
},
];
THE AUTHORS ARRAY OF OBJECTS:
const authors = [
{
id: '27cc3006-e93a-4748-8ca8-73d06aa93b6d',
name: 'Vasiliy Dobkin',
},
{
id: 'f762978b-61eb-4096-812b-ebde22838167',
name: 'Nicolas Kim',
},
{
id: 'df32994e-b23d-497c-9e4d-84e4dc02882f',
name: 'Anna Sidorenko',
},
{
id: '095a1817-d45b-4ed7-9cf7-b2417bcbf748',
name: 'Valentina Larina',
},
];
MY TRIES TO MATCH AND "DE-HASH" THE AUTHORS:
const COURSE = props.value;
// AUTHOR = authors.find((el) => el.id === COURSE.authors[0]);
// const filteredArray = array1.filter(value => array2.includes(value));
// arrA.filter(x => arrB.includes(x));
const authorsDeHash = (authorsHashed, authors) => {
// const myarr = [];
for (let i of authorsHashed) {
// console.log(authorsHashed);
console.log(authors.find((el) => el.id === authorsHashed[i]));
// myarr.push(authors.find((el) => el.id === authorsHashed[i]));
}
};
authorsDeHash(COURSE.authors, authors);
UPD: since I'm already passing a ONE single course from the parent component, I don't need to search for the value in entire "courses" array. So basically I have just to find authors real names for a one course.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
可能的方法如下:
创建一个基于作者ID 映射实例通过相应地
地图
ped作者
array。地图
课程
阵列课程项目每个课程中每个课程项目的ID基于<代码>作者数组被映射到基于作者名称中通过从作者的ID中查找以前创建的authorsMap
的阵列。A possible approach is as follows ...
create an author id based Map instance by passing an accordingly
map
pedauthors
array.map
thecourses
array course items each into a new item where each course item's id basedauthors
array gets mapped into an author name based array by looking up the latter from the before createdauthorsMap
by an author's id.我为循环使用了
和
array.prototype.find()
函数。请参阅下面的摘要。I used a
for
loop and theArray.prototype.find()
function. Please see snippet below.因此,我就是这样做的。主要问题是循环,大声笑。而不是(让我使用的i)我使用过(让我在j中)。
感谢您的答案:)
So simply I've done it that way. The main problem was in the loop, lol. Instead of (let i of j) I used (let i in j).
Thanks for the answers :)