How to design a more efficient loop using underscore + ES 6

Below are my two arrays.

let clientCollection = ["1","ABC","X12","OE2","PQ$"];



let serverCollection = [{
    "Id": "1",
    "Name": "Ram",
    "Other": "Other properties"

},
{
    "Id": "ABC",
    "Name": "Shyam",
    "Other": "Other properties"

},
{
    "Id": "OE2",
    "Name": "Mohan",
    "Other": "Other properties"

}]

Now I need to compare the two collections above and create two for the arrays

let matchedIds = []; 

let unMatchedIds = [];

Now this is what I am doing now.

for(let i =0 ; i < clientsCollection.length;i++)
{
    if(_.indexOf(serverCollection, clientCollection[i]) >= 0)
    {
          matchedIds.push(clientCollection[i]);
    }
    else
    { 
        unMatchedIds.push(clientCollection[i]);
    }
}

In my application, the size of these arrays can increase to 1000 or more. This can have effective problems.

I use underscore and try if I can get a better solution but can't find.

Can someone please suggest if I can do the same thing more efficiently using underscore + ES6 ??

+4
source share
3 answers

I think this would be a good way to populate matchedIds:

for(let i = serverCollection.length - 1; i >= 0; i--) {
  const id = serverCollection[i]['Id'];
  if(clientCollection.indexOf(id) !== -1) {
    matchedIds.push(id);
  }
}

unMatchedIds matchedIds:

for (var i = clientCollection.length - 1; i >= 0; i--) {
  if (matchedIds.indexOf(clientCollection[i]) === -1) {
    unMatchedIds.push(clientCollection[i]);
  }
}

filter, reduce .. , indexOf!

UPD : https://plnkr.co/edit/UcOv6SquUgC7Szgfn8Wk?p=preview. , 10000 5 , 2 , .

+1

Set . , Set :

let serverCollection = [
  {
    Id: '1',
    Name: 'Ram',
    Other: 'Other properties'
  },
  {
    Id: 'ABC',
    Name: 'Shyam',
    Other: 'Other properties'
  },
  {
    Id: 'OE2',
    Name: 'Mohan',
    Other: 'Other properties'
  }
];

let clientCollection = ['1', 'ABC', 'X12', 'OE2', 'PQ$'];
const serverIds = new Set(serverCollection.map((server) => server.Id));

let matchedIds = [];
let unmatchedIds = [];

for (let id of clientCollection) {
  if (serverIds.has(id)) {
    matchedIds.push(id);
  } else {
    unmatchedIds.push(id);
  }
}

console.log('matched', matchedIds);
console.log('unmatched', unmatchedIds);

clientCollection serverCollection .

. plunkr

+1

Create a Set of Server Identifiers. Use Array # to reduce to iterate the clients, and assign an identifier to the auxiliary array according to its existence in the server set. Extract auxiliary arrays into variables using a destructuring assignment .

const clientCollection = ["1","ABC","X12","OE2","PQ$"];

const serverCollection = [{"Id":"1","Name":"Ram","Other":"Other properties"},{"Id":"ABC","Name":"Shyam","Other":"Other properties"},{"Id":"OE2","Name":"Mohan","Other":"Other properties"}];

// create a set of Ids. You can use underscore pluck instead of map
const serverSet = new Set(serverCollection.map(({ Id }) => Id));

// reduce the ids to an array of two arrays (matchedIds, unMatchedIds), and then get assign to variables using destructuring assignment
const [matchedIds, unMatchedIds] = clientCollection.reduce((r, id) => {
  r[serverSet.has(id) ? 0 : 1].push(id); // push to a sub array according to existence in the set
  
  return r;
}, [[], []])

console.log(matchedIds);

console.log(unMatchedIds);
Run code
0
source

Source: https://habr.com/ru/post/1687417/


All Articles