More efficient way search for multiple objects in array

Jialx

I am currently using a for loop and multiple if statements to count the number of records that match a string.

for (var i = 0; i < DailyLogs.length; i++) {
  if (DailyLogs[i].created_at >= this.state.startMonth && DailyLogs[i].created_at <= this.state.finishMonth) {
    if (DailyLogs[i].created_by_id === "37aa0778-c148-4c04-b239-18885d46a8b0" ) { md1++; }
    if (DailyLogs[i].created_by_id === "869a7967-ffb3-4a20-b402-ad6d514472de" ) { md2++; }
    if (DailyLogs[i].created_by_id === "92c0f155-ce82-4b50-821f-439428c517a3" ) { md3++; }
    if (DailyLogs[i].created_by_id === "aa9eb0f2-35af-469a-8893-fc777b444bed" ) { md4++; }
    if (DailyLogs[i].created_by_id === "967d63ea-492c-4475-8b08-911be2d0bf22" ) { md5++; }
    if (DailyLogs[i].created_by_id === "47ec8d60-1fa2-4bf5-abc8-34df6bd53079" ) { md6++; }
    if (DailyLogs[i].created_by_id === "92c0f155-ce82-4b50-821f-439428c517a3" ) { md7++; }
  }
}

Is there a better way to do this, apart from having multiple if statements, or perhaps shorting this somehow.

T.J. Crowder

You can make it (slightly) more efficient by using else if, since if an earlier if's condition is true, by definition later ones can't be.

Long series of else if in JavaScript can be written as switch instead.

for (var i = 0; i < DailyLogs.length; i++) {
  if (DailyLogs[i].created_at >= this.state.startMonth && DailyLogs[i].created_at <= this.state.finishMonth) {
      switch (DailyLogs[i].created_by_id) {
        case "37aa0778-c148-4c04-b239-18885d46a8b0": md1++; break;
        case "869a7967-ffb3-4a20-b402-ad6d514472de": md2++; break;
        case "92c0f155-ce82-4b50-821f-439428c517a3": md3++; break;
        case "aa9eb0f2-35af-469a-8893-fc777b444bed": md4++; break;
        case "967d63ea-492c-4475-8b08-911be2d0bf22": md5++; break;
        case "47ec8d60-1fa2-4bf5-abc8-34df6bd53079": md6++; break;
        case "92c0f155-ce82-4b50-821f-439428c517a3": md7++; break;
      }
  }
}

Using else if vs. switch is largely a matter of style in cases like this.

Another option is to maintain your counters in an object keyed by the created_by_id:

var md = {
    "37aa0778-c148-4c04-b239-18885d46a8b0": 0,
    "869a7967-ffb3-4a20-b402-ad6d514472de": 0,
    "92c0f155-ce82-4b50-821f-439428c517a3": 0,
    "aa9eb0f2-35af-469a-8893-fc777b444bed": 0,
    "967d63ea-492c-4475-8b08-911be2d0bf22": 0,
    "47ec8d60-1fa2-4bf5-abc8-34df6bd53079": 0,
    "92c0f155-ce82-4b50-821f-439428c517a3": 0
};
for (var i = 0; i < DailyLogs.length; i++) {
  if (DailyLogs[i].created_at >= this.state.startMonth && DailyLogs[i].created_at <= this.state.finishMonth && md.hasOwnProperty(DailyLogs[i].created_by_id)) {
      md[DailyLogs[i].created_by_id]++;
  }
}

You can also avoid the repeated DailyLogs[i] by using a variable in the loop body:

var log = DailyLogs[i];
if (log.created_at >= ...)

In modern ES2015+ environments, you can combine for-of with destructuring:

for (const {created_at, created_by_id} of DailyLogs) {
  if (created_at >= this.state.startMonth && created_at <= this.state.finishMonth) {
      switch (created_by_id) {
        case "37aa0778-c148-4c04-b239-18885d46a8b0": md1++; break;
        case "869a7967-ffb3-4a20-b402-ad6d514472de": md2++; break;
        case "92c0f155-ce82-4b50-821f-439428c517a3": md3++; break;
        case "aa9eb0f2-35af-469a-8893-fc777b444bed": md4++; break;
        case "967d63ea-492c-4475-8b08-911be2d0bf22": md5++; break;
        case "47ec8d60-1fa2-4bf5-abc8-34df6bd53079": md6++; break;
        case "92c0f155-ce82-4b50-821f-439428c517a3": md7++; break;
      }
  }
}

Those options can be combined in various ways, for instance:

const md = {
    "37aa0778-c148-4c04-b239-18885d46a8b0": 0,
    "869a7967-ffb3-4a20-b402-ad6d514472de": 0,
    "92c0f155-ce82-4b50-821f-439428c517a3": 0,
    "aa9eb0f2-35af-469a-8893-fc777b444bed": 0,
    "967d63ea-492c-4475-8b08-911be2d0bf22": 0,
    "47ec8d60-1fa2-4bf5-abc8-34df6bd53079": 0,
    "92c0f155-ce82-4b50-821f-439428c517a3": 0
};
for (const {created_at, created_by_id} of DailyLogs) {
  if (created_at >= this.state.startMonth && created_at <= this.state.finishMonth && md.hasOwnProperty(created_by_id)) {
      md[created_by_id]++;
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is there a more efficient way to sort this array?

Is there a more efficient way to add to an Array?

Efficient design to search for objects by multiple parameters with range

A more efficient way to rearrange Array of Objects?

More efficient way to add multiple button groups

More efficient way of writing multiple React components?

Is there a more efficient way to convert a multiple line of string to a numpy array?

Is there a more efficient way to store containers of objects in Access?

A more efficient way to iterate over multiple DataFrames

More Efficient Way to Avoid Multiple Calculations?

More Efficient Way to Avoid Multiple Calculations #2?

More Efficient Way to Avoid Multiple Calculations #3?

More Efficient Way to Avoid Multiple Calculations #4?

Using multiple filter on multiple columns of numpy array - more efficient way?

Is there a more efficient way to group this by array?

More efficient array search

Is there a more efficient way to do scrabble search with grep?

More Efficient Way to Create array

Which way of rendering objects in OpenGL is more efficient?

Is there a more efficient way to write multiple if else?

More efficient way to create an array of objects using puppeteer

More efficient way to assign multiple variables in a FOR loop?

Efficient way to group objects in an Array

More efficient way to update multiple model objects each with unique values

Is there a more efficient way to search an image for a specific pixel?

Is there a more efficient way instead of multiple FOR loops and IF statements?

What is the most efficient way to convert JSON with multiple objects to array in JavaScript

Most efficient way to use an array of objects to query more objects - MongoDb?

A more efficient way of creating an NxM array in Python