How to check a specific line in a dictionary?

I have a dictionary like:

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

How can I find this if the dictionary contains "----" in any of the key values.
Any javascript function?
EDIT: What if this is so?

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

He gives this error:

 TypeError: a[elem].indexOf is not a function 
+4
source share
4 answers

Here is the code:

 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); }; } 

EDIT: Changed the iteration over the array to normal after the comment.

+2
source

Use indexOf to search for each array in an object:

 for (elem in a) { if (a[elem].indexOf("----") != -1) alert('---- found at ' + a[elem]); } 

EDIT For this error: TypeError: a[elem].indexOf is not a function browser might consider that the empty element is a non-string type; the non-string type does not have an indexOf method.

This code checks the length of an array element (if the element is empty before interpreting the indexOf function.

 for (elem in a) { if (a[elem].length > 0 && a[elem].indexOf("----") != -1) alert('---- found at ' + a[elem]); } 

If you want to support IE <9, see this post to conditionally add an indexOf definition to an Array object. The post also mentions an alternative to jQuery.

The SO post mentioned above represents this version of the Mozilla indexOf function.

 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; }; } 
+1
source

If you know exactly the nesting level of your value, a quick solution is possible (as suggested in other answers).

However, if you need a deep crawl search, you will need a recursive version of the solutions, for example:

 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; } 

Examples:

 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 

However, if you are looking for a more thorough solution, use a framework like jsTraverse

+1
source

Use Object.getOwnPropertyNames () .

You need to write two nested loops. With Object.getOwnPropertyNames you get access to an array that consists of object property names. Then you will need to iterate over the value of these properties and determine the correct element inside this second array.

 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 ----") }; } } 
0
source

Source: https://habr.com/ru/post/1490685/


All Articles