Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 要在对象数组中搜索字符串吗?_Javascript - Fatal编程技术网

Javascript 要在对象数组中搜索字符串吗?

Javascript 要在对象数组中搜索字符串吗?,javascript,Javascript,我想搜索这个由对象组成的employee数组,我应该能够搜索任何文本,就像我在数组中传递-->搜索字符串(“aaron”,employees);它应该显示“数组中存在值”,或者如果是这样,请帮助我 //here is the employeee array ... var employees =[{ name:"jacob", age :23,

我想搜索这个由对象组成的employee数组,我应该能够搜索任何文本,就像我在数组中传递-->搜索字符串(“aaron”,employees);它应该显示“数组中存在值”,或者如果是这样,请帮助我

//here is the employeee  array ...
  var employees =[{
                          name:"jacob",
                          age :23,
                          city:"virginia",
                          yoe :12,
                          image :'a.jpg'
                       },
                       {
                          name:"aaron",
                          age :21,
                          city:"virginia",
                          yoe :12,
                          image :'b.jpg'
                       },
                       {
                          name:"johnny",
                          age :50,
                          city:"texas",
                          yoe :12,
                          image :'c.jpg'
                       },
                       {
                          name:"jacob",
                          age :12,
                          city:"virginia",
                          yoe :12,
                          image :'a.jpg'
                       }];
下面是在数组中执行搜索功能的函数

    function search_for_string_in_array(search_for_string, employees) 
            {
                for (var i=0; i < employees.length; i++) 
                {
                    if (employees[i].match(search_for_string))
                    { 
                        return 'Value exists in array';
                    }
                }
               return 'Value does NOT exist in array';
            }
函数搜索\u数组中的\u字符串(搜索\u字符串,员工)
{
对于(变量i=0;i

只需将要搜索的值和数组传递给函数,它就会告诉您字符串是否作为数组值的一部分存在。

如果您知道数组的结构,您可能只需循环遍历它并测试每个值。如果您不知道该结构,可以将该数组字符串化为JSON和regexp(尽管它不会那么安全):

只需更换线路

if (employees[i].match(search_for_string))
为此:

if (employees[i].name.match(search_for_string))
试一试

函数搜索(文本){
文本+=”;
变量i,obj;
var数组=[];
对于(i=0;i=0
||(''+键).indexOf(文本)>=0){
阵列推送(obj);
打破
}
}
}
}
返回array.length>0;
}

演示:

…假设“name”是您要查找的唯一键。您说得对,这样他就可以添加另一个参数-数组,该数组包含他要查看的所有键。我真的不明白他的问题-我以为他只是在问对象是如何工作的…嘿,david thnx…但它只搜索值,它确实会给出键的结果,比如如果对象有属性-->名称:“jacob”,当你传递它时它返回true,但当你传递“name”时它返回true它返回False如果你想搜索关键字,只需删除冒号即可
'\“'+str+'\'”
Thnx-Arun但是它的jst给了我一个对象的“值”,而不是“key”,假设我们有{name:“jacob”},当你传递它jacob时,它返回true,但当你传递“name”时,它返回true它回来了false@user1948425当您传递
name
它应该返回什么时,它应该返回true,因为它存在于object@user1948425所以在前面的例子中,
jacob
name
都应该返回trueyeah…我使用函数search\u在数组(str,arr)中搜索字符串{var json=json.stringify(arr);返回新的RegExp('\“'+str+'\“','g').test(json);}但它包含正则表达式,我希望不包含它
if (employees[i].name.match(search_for_string))
function search(text) {
    text += '';
    var i, obj;
    var array = [];
    for (i = 0; i < employees.length; i++) {
        var obj = employees[i];
        for (var key in obj) {
            if (obj.hasOwnProperty(key)) {
                if (('' + obj[key]).indexOf(text) >= 0
                        || ('' + key).indexOf(text) >= 0) {
                    array.push(obj);
                    break;
                }
            }
        }
    }

    return array.length > 0;
}