Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.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_Function - Fatal编程技术网

Javascript 如何检查字典中的特定字符串?

Javascript 如何检查字典中的特定字符串?,javascript,function,Javascript,Function,我有一本字典,像: a = {"staticData":['----','Blue','Green'], "inData":['Indatahere','----','----']} 如果字典在任何键的值中包含“----”,我如何才能找到它。 有Javascript函数吗? 编辑: 如果情况是这样呢 a = {"staticData":[], "inData":['Indatahere','----','----']} 它给出了以下错误: TypeError: a[elem].indexOf

我有一本字典,像:

a = {"staticData":['----','Blue','Green'], "inData":['Indatahere','----','----']}
如果字典在任何键的值中包含“----”,我如何才能找到它。
有Javascript函数吗?
编辑: 如果情况是这样呢

a = {"staticData":[], "inData":['Indatahere','----','----']}
它给出了以下错误:

TypeError: a[elem].indexOf is not a function
代码如下:

var a = {"staticData":['----','Blue','Green'], "inData":['Indatahere','----','----']};

for(var key in a){
    var value = a[key];
    for(var i=0; i<value.length; i++){
        if(value[i] == '----') alert("Found '----' in '" + key + "' at index " + i);
    };
}
var a={“staticData”:['-','Blue','Green'],“inData”:['Indatahere','-','-']};
for(a中的var键){
var值=一个[键];

对于(var i=0;i使用
indexOf
搜索对象中的每个数组:

for (elem in a)
{
    if (a[elem].indexOf("----") != -1)
       alert('---- found at ' + a[elem]);
}
编辑 对于此错误:
TypeError:a[elem].indexOf不是函数
浏览器可能会将空元素视为非字符串类型;非字符串类型没有indexOf方法

此代码检查数组元素的长度(如果在解释
indexOf
函数之前元素为空)

for (elem in a)
{
    if (a[elem].length > 0 && a[elem].indexOf("----") != -1)
       alert('---- found at ' + a[elem]);
}
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length >>> 0;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}
如果您希望支持IE<9,请参阅有条件地向数组对象添加indexOf定义

上面提到的SO帖子列出了这个Mozilla版本的
indexOf
函数

for (elem in a)
{
    if (a[elem].length > 0 && a[elem].indexOf("----") != -1)
       alert('---- found at ' + a[elem]);
}
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length >>> 0;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}
if(!Array.prototype.indexOf)
{
Array.prototype.indexOf=函数(elt/*,from*/)
{
var len=this.length>>>0;
var from=Number(参数[1])| | 0;
from=(from<0)
?数学单元(来自)
:数学。地板(从);
如果(从<0开始)
from+=len;
for(;from
如果您确切知道您的值的嵌套级别,那么快速解决方案(如其他答案所示)是可能的

但是,如果需要深入遍历搜索,则需要解决方案的递归版本,例如:

function FindTraverse(data, match)
{
    for (var prop in data)
    {
         if (!data.hasOwnProperty(prop)) continue;
         if (data[prop] == match) return true;
         if (typeof data[prop] == 'object' && FindTraverse(data[prop], match)) return true;
    }

    return false;
}
示例:

FindTraverse({a:'Foo',b:'Bar'}, 'Bar') // true
FindTraverse(['Foo','Bar'], 'Bar') // true
FindTraverse([{name:'Foo'},{name:'Bar'}], 'Bar') // true
FindTraverse({a:{name:'FooBar'},b:'Bar'}, 'FooBar') // true
但是,如果您正在寻找更彻底的解决方案,请使用类似于use的框架

您必须编写两个嵌套循环。使用Object.getOwnPropertyNames,您将访问一个由对象的属性名称组成的数组。然后,您将需要循环这些属性的值,并在第二个数组中标识正确的元素

a = {"staticData":['----','Blue','Green'], "inData":['Indatahere','----','----']}

props = Object.getOwnPropertyNames(a);

for (i=0;i < props.length;i ++) {
    for (z = 0; z < a[props[i]].length; z ++) {
        //console.log(a[props[i]][z])
        if ( (a[props[i]][z]) == '----') {
            console.log("I have found an item with ----")
        };
    }
}
a={“staticData”:['-'、'Blue'、'Green']、'inData:['Indatahere'、'-'、'-']}
props=Object.getOwnPropertyNames(a);
对于(i=0;i
很好,所以对你来说+1。你觉得我的stackoverflow.com/questions/17567925/javascript-how-to-check-for-specific-string-in-a-dictionary/17568064#answer-17568064怎么样?值得一提的是,
Array.prototype.indexOf
没有在IE<9中实现。如果你需要支持旧版本,你需要填充这个函数。请参阅@Tommi感谢您提供的信息,帖子更新了相关的SO链接。@猜疑:请检查更新的问题并帮助我!@MHS您必须使用IE吗?如果是,您需要添加列出的代码(取自另一篇SO帖子)在脚本的顶部。你只是重新实现了
indexOf
。很糟糕,因为在
中使用
for..in来迭代数组是个坏主意。@FlorianMargaine,我在你的评论后更新了我的代码。请注意
indexOf
只返回第一次出现的内容,所以我的代码片段不是
indexOf的重新实现
。当然可以使用
indexOf
实现所有发生查找,但是第二个
for loop
也是必需的,尽管它看起来不同。因为我们迭代了
value
列表,索引已经在板上了。