gpt4 book ai didi

javascript - 我如何搜索整个数组以找到它的键?

转载 作者:行者123 更新时间:2023-11-28 07:34:24 25 4
gpt4 key购买 nike

如何以快速/优化的方式从如下所示的大型数组列表中找到 key ?

var error_constant_ENGLISH = {
group0: {'xyz','1234'},

group1: {'Temporarily Unavailable',
'Invalid number',
'You have reached the voice mail of P P P',
'We are sorry then number you have dialed is not valid please try again',
'Large sentences'},

group2: {'abc','def large sentences'}
}

var error_constant_FRENCH = {
group0: {'xyz','1234'},

group1: {'Temporarily Unavailable',
'Invalid number',
'You have reached the voice mail of P P P',
'We are sorry then number you have dialed is not valid please try again',
'Large sentences'},

group2: {'abc','def large sentences'}
}

// Assume:
var language='ENGLISH';
var reason_phrase='Invalid number'
search_in_error(reason_phrase);

// Expected output: group1

最佳答案

使用这样的数据结构,它不可能比 O(n x m) 更快。假设错误组是数组。

function search_in_error(phrase){
var errorGroup = window["error_constant_" + language],
errors;
for(var key in errorGroup){
if (!errorGroup.hasOwnProperty(key)){
continue;
}
errors = errorGroup[key];
for (var i = 0; i < errors.length; i++){
if (errors[i] === phrase)
return key;
}
}
return null;
}

http://jsfiddle.net/fsd63jf2/

关于javascript - 我如何搜索整个数组以找到它的键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28763559/

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