skip to Main Content
let L1 = [
    { id: 1, name: 'Object 1' },
    { id: 1, name: 'Object 2' },
    { id: 1, name: 'Object 3' },

    { id: 2, name: 'Object 1' },
    { id: 2, name: 'Object 2' },
    
    { id: 3, name: 'Object 1' },
    { id: 3, name: 'Object 3' }  
];

// Second array
let L2 = ['Object 1', 'Object 3'];

let idsWithMissingNames = L1.reduce((result, item) => {
  if (!L2.includes(item.name)) {
      result.push(item.id);
  }
  return result;
}, []);

console.log(idsWithMissingNames);
//expected result is: 2

I included my code as well but it doesn’t work.

2

Answers


  1. Since it is an exclusion AND check, you need to do some pre processing to get the data into a format that it is easy to loop over and find the matches. So create an object with arrays that holds the name.

    You will loop over this object and look for all the keys that match. If they all match you exclude it.

    let L1 = [
        { id: 1, name: 'Object 1' },
        { id: 1, name: 'Object 2' },
        { id: 1, name: 'Object 3' },
    
        { id: 2, name: 'Object 1' },
        { id: 2, name: 'Object 2' },
        
        { id: 3, name: 'Object 1' },
        { id: 3, name: 'Object 3' }  
    ];
    
    let L2 = ['Object 1', 'Object 3'];
    
    // combine all of he names into an array so it is easy look up
    const groupedNamesById = L1.reduce((acc, obj) => {
        acc[obj.id] = acc[obj.id] || [];
        acc[obj.id].push(obj.name);
        return acc;
    }, {});
    
    // Loop over your groups of ids to find the items that do not have all matches
    const nonMatchIds = Object.entries(groupedNamesById).reduce((ids, entry) => {
      // do all of the names exist for this group? If no, add it to the list
      if (!L2.every(name => entry[1].includes(name))) {
        ids.push(entry[0]);
      }
      return ids;
    }, []);
    
    // Display your group
    console.log(nonMatchIds);
    Login or Signup to reply.
  2. Collect counts and filter ones less than L2’s length:

    let L1 = [
        { id: 1, name: 'Object 1' },
        { id: 1, name: 'Object 2' },
        { id: 1, name: 'Object 3' },
    
        { id: 2, name: 'Object 1' },
        { id: 2, name: 'Object 2' },
        
        { id: 3, name: 'Object 1' },
        { id: 3, name: 'Object 3' }  
    ];
    
    // Second array
    let L2 = ['Object 1', 'Object 3'];
    
     
    const counts = L1.reduce((r, item) => ((r[item.id] ??= [item.id, 0])[1] += L2.includes(item.name), r), {});
    let idsWithMissingNames = Object.values(counts).reduce((r, [id, count]) => (count < L2.length && r.push(+id), r), []);
    
    console.log(idsWithMissingNames);
    //expected result is: 2
    ` Chrome/117
    -------------------------------------------------------
    Alexander    1.0x  |  x1000000  229  234  239  241  243
    epascarelo   1.8x  |  x1000000  402  409  411  416  452
    -------------------------------------------------------
    https://github.com/silentmantra/benchmark `
    
    let L1 = [
        { id: 1, name: 'Object 1' },
        { id: 1, name: 'Object 2' },
        { id: 1, name: 'Object 3' },
    
        { id: 2, name: 'Object 1' },
        { id: 2, name: 'Object 2' },
        
        { id: 3, name: 'Object 1' },
        { id: 3, name: 'Object 3' },
        
        { id: 4, name: 'Object 1' },
        { id: 4, name: 'Object 2' },
        { id: 4, name: 'Object 3' },
    
        { id: 5, name: 'Object 1' },
        { id: 5, name: 'Object 2' },
        
        { id: 6, name: 'Object 1' },
        { id: 6, name: 'Object 3' },
    ];
    
    // Second array
    let L2 = ['Object 1', 'Object 3'];
    
    
    // @benchmark epascarelo
    const groupedNamesById = L1.reduce((acc, obj) => {
        acc[obj.id] = acc[obj.id] || [];
        acc[obj.id].push(obj.name);
        return acc;
    }, {});
    
    Object.entries(groupedNamesById).reduce((ids, entry) => {
      if (!L2.every(name => entry[1].includes(name))) {
        ids.push(entry[0]);
      }
      return ids;
    }, []);
    
    
    // @benchmark Alexander
    const counts = L1.reduce((r, item) => ((r[item.id] ??= [item.id, 0])[1] += L2.includes(item.name), r), {});
    Object.values(counts).reduce((r, [id, count]) => (count < L2.length && r.push(+id), r), []);
    /*@end*/eval(atob('e2xldCBlPWRvY3VtZW50LmJvZHkucXVlcnlTZWxlY3Rvcigic2NyaXB0Iik7aWYoIWUubWF0Y2hlcygiW2JlbmNobWFya10iKSl7bGV0IHQ9ZG9jdW1lbnQuY3JlYXRlRWxlbWVudCgic2NyaXB0Iik7dC5zcmM9Imh0dHBzOi8vY2RuLmpzZGVsaXZyLm5ldC9naC9zaWxlbnRtYW50cmEvYmVuY2htYXJrL2xvYWRlci5qcyIsdC5kZWZlcj0hMCxkb2N1bWVudC5oZWFkLmFwcGVuZENoaWxkKHQpfX0='));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search