(JavaScript)在不同的对象数组中找到相同的值

发布于 2025-02-05 03:01:33 字数 2575 浏览 1 评论 0原文

我正在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青瓷清茶倾城歌 2025-02-12 03:01:33
const authorsMap = authors.reduce((acc, author) => ({ ...acc, [author.id]: author.name }), {}); // it faster to find a value in a Map than searching in Array

const coursesWithAuthors = courses
.map(course => ({ ...course, authors: course.authors.map(id => authorsMap[id]) }));
const authorsMap = authors.reduce((acc, author) => ({ ...acc, [author.id]: author.name }), {}); // it faster to find a value in a Map than searching in Array

const coursesWithAuthors = courses
.map(course => ({ ...course, authors: course.authors.map(id => authorsMap[id]) }));
浅忆流年 2025-02-12 03:01:33

可能的方法如下:

  • 创建一个基于作者ID 映射实例通过相应地 地图 ped 作者 array。

  • 地图 课程阵列课程项目每个课程中每个课程项目的ID基于<代码>作者数组被映射到基于作者名称中通过从作者的ID中查找以前创建的authorsMap的阵列。

const courses = [{
  id: 'de5aaa59-90f5-4dbc-b8a9-aaf205c551ba',
  title: 'JavaScript',
  description: `Lorem Ipsum is simply dummy text`,
  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 Ipsumchas been the industry's standard dummy tex`,
  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',
}];

// create an author id based map.
const authorsMap =
  new Map(authors.map(({ id, name }) => [id, name]));

// map each course item and for each course item's
// `authors` array map each author's id into the related
//  author name by looking it up from the `authorsMap`.
console.log(
  courses
    .map(({ authors, ...courseRest }) => ({
      ...courseRest,
      authors: authors.map(authorId =>
        authorsMap.get(authorId)
      ),
    }))
);
console.log(
  'the non mutated original `courses` array ...',
  courses,
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

A possible approach is as follows ...

  • create an author id based Map instance by passing an accordingly mapped authors array.

  • map the courses array course items each into a new item where each course item's id based authors array gets mapped into an author name based array by looking up the latter from the before created authorsMap by an author's id.

const courses = [{
  id: 'de5aaa59-90f5-4dbc-b8a9-aaf205c551ba',
  title: 'JavaScript',
  description: `Lorem Ipsum is simply dummy text`,
  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 Ipsumchas been the industry's standard dummy tex`,
  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',
}];

// create an author id based map.
const authorsMap =
  new Map(authors.map(({ id, name }) => [id, name]));

// map each course item and for each course item's
// `authors` array map each author's id into the related
//  author name by looking it up from the `authorsMap`.
console.log(
  courses
    .map(({ authors, ...courseRest }) => ({
      ...courseRest,
      authors: authors.map(authorId =>
        authorsMap.get(authorId)
      ),
    }))
);
console.log(
  'the non mutated original `courses` array ...',
  courses,
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

狠疯拽 2025-02-12 03:01:33

我为循环使用了array.prototype.find()函数。请参阅下面的摘要。

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 = courses[0]
    const authorsDeHash = (authorsHashed, authors) => {
        for (let i = 0; i < authorsHashed.length; i++) {
          let author = authors.find(e => e.id === authorsHashed[i]);
          console.log(author.name)
        }
    };
    authorsDeHash(COURSE.authors, authors);

I used a for loop and the Array.prototype.find() function. Please see snippet below.

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 = courses[0]
    const authorsDeHash = (authorsHashed, authors) => {
        for (let i = 0; i < authorsHashed.length; i++) {
          let author = authors.find(e => e.id === authorsHashed[i]);
          console.log(author.name)
        }
    };
    authorsDeHash(COURSE.authors, authors);

一笑百媚生 2025-02-12 03:01:33

因此,我就是这样做的。主要问题是循环,大声笑。而不是(让我使用的i)我使用过(让我在j中)。

const authorsDeHash = (authorsHashed, authorsOrigin) => {
        const arr = [];
        for (let i in authorsHashed)
            arr.push(authorsOrigin.find((el) => el.id === authorsHashed[i]).name);
        return arr;
    };
    const authorsArray = authorsDeHash(course.authors, authors);

    const printAuthors = (arr) => {
        const obj = { title: '', authors: '' };
        if (arr.length === 1) {
            obj.title = 'Author:';
            obj.authors = arr[0];
        } else {
            obj.title = 'Authors:';
            obj.authors = `${arr.join(', ')}`;
        }
        return obj;
    };
    const authorsObj = printAuthors(authorsArray);

感谢您的答案:)

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).

const authorsDeHash = (authorsHashed, authorsOrigin) => {
        const arr = [];
        for (let i in authorsHashed)
            arr.push(authorsOrigin.find((el) => el.id === authorsHashed[i]).name);
        return arr;
    };
    const authorsArray = authorsDeHash(course.authors, authors);

    const printAuthors = (arr) => {
        const obj = { title: '', authors: '' };
        if (arr.length === 1) {
            obj.title = 'Author:';
            obj.authors = arr[0];
        } else {
            obj.title = 'Authors:';
            obj.authors = `${arr.join(', ')}`;
        }
        return obj;
    };
    const authorsObj = printAuthors(authorsArray);

Thanks for the answers :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文