gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 18:14:54 24 4
gpt4 key购买 nike

这是我的代码

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com