What's the best way to find the same fields in arrays and objects?

  • 0
    What is the best way to check for the presence of the same id when adding?

    let exampleArray = 
    [{id:28, container: json},
    {id:52, container: json},
    {id:28, container: json}];


    Only one solution comes, through a loop comparing id.
    It seems this is not the best solution, maybe there are more ways?
    I am just learning JS, now I am practicing :)
    JavaScript Lincoln Ayers, Jun 22, 2019

  • 2 Answers
  • 0
    in the sense of unique id?

    const uniqueArray = [...new Set(exampleArray.map(item => item.id))] // [28, 52]


    well, in general ...
    L et

    container: json

    SCArr

    addid

    cc

    'false'

    ...


    Anonymous

  • 0
    const uniqIds = new Set();
    const result = exampleArray.filter(({ id }) => {
    if (uniqIds.has(id)) {
    return false;
    }
    uniqIds.add(id);
    return true;
    });
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!