javascript - 使数组上的值匹配更快?

标签 javascript arrays angular performance string-matching

这是我的代码

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  devices = ['x', 'y', 'z'];
  device;
  constructor(public navCtrl: NavController) {
    this.device = 'a';
    for(let i = 0; i<this.devices.length; i++){
      if(this.device == this.devices[i]){
        console.log('Match');
      } else {
        console.log('No matches');
      }
    }
  }
}

我在想,如果我的设备列表变得太长,那么匹配会变得非常慢。所以我想问一下是否有更好、更快、更有效的方法来检查数组中是否存在值。

我要实现的是出勤率。

我的应用程序将扫描 ID,然后检查它是否在我的列表中。 如果匹配,我会将 bool 值设置为 true(用于测试目的) 所述 bool 值将在我的列表中。 像这样

device = {
 name: '-K8d34fsd2',
 attendance: true
};

这是我尝试过的

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  devices = [
    {
      id: 'qwerty123',
      attendance: false
    },
    {
      id: 'zxcvb123',
      attendance: false
    },
  ];
  device;
  constructor(public navCtrl: NavController) {
    this.device = 'qwerty123';
    // for(let i = 0; i<this.devices.length; i++){
    //   if(this.device == this.devices[i]){
    //     console.log('Match');
    //   } else {
    //     console.log('No matches');
    //   }
    // }

      if(this.devices.id.indexOf(this.device) > -1){
        console.log('Match');
      } else {
        console.log('No matches');
      }
  }
}

最佳答案

你有一些选择。

Array.indexOf() 这比遍历整个数组并检查每个元素是否符合条件要好得多 ( slighthly faster using your example on a benchmark )。

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  devices = ['x', 'y', 'z'];
  device;
  constructor(public navCtrl: NavController) {
    this.device = 'a';
    if(this.devices.indexOf(this.device) > -1){
        console.log('Match');
    } else {
        console.log('No matches');
    }

  }
}

如果你能支持 ES2016,Array.includes()indexOf() ( see the same benchmark ) 稍微好一点,语法上更容易阅读。

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  devices = ['x', 'y', 'z'];
  device;
  constructor(public navCtrl: NavController) {
    this.device = 'a';
    if(this.devices.includes(this.device)){
        console.log('Match');
    } else {
        console.log('No matches');
    }

  }
}

编辑:由于 OP 对原始问题的编辑,我将列出使用对象进行搜索的可能方法:

1 - for 循环:

var test = [{name:'asdfasafdx', attendance: true}, {name:'fdsafdsay', attendance: true}, {name:'sdfasdfasz', attendance: true}];

var device = {name:'fdwoierqea'};

for(let i = 0; i < test.length; i++){
      if(device.name == test[i].name){
        console.log('Match');
      } else {
        console.log('No matches');
      }
}

2 - Array.filter() ( faster )

var test = [{name:'asdfasafdx', attendance: true}, {name:'fdsafdsay', attendance: true}, {name:'sdfasdfasz', attendance: true}];

var device = {name:'fdwoierqea'};

if(test.filter(value => value.name === device.name).length > 0){
        console.log('Match')

} else {
        console.log('No matches');
}

关于javascript - 使数组上的值匹配更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51977231/

相关文章:

javascript - Angular:导出类和公共(public)类之间的区别?

java - 为什么使用 boolean 数组需要这么长的时间?

java - 循环切掉数组的一个元素

php - 使用 file_put_contents 的数组

angular - "takeWhile is not a function"与 IntervalObservable 一起使用时

angular - Angular/CLI 6.x 中的自定义原理图构建没有错误,但我无法使用它来生成文件

javascript - 使用 jQuery 获取调整大小的图像高度

javascript - 使用 Snap.Select 时 SVG 未捕获类型错误

javascript - 电子邮件中的字符串到 html 元素

javascript - 使用外部服务验证表单