Hi everyone,

I am trying yo loop through an array property that is multi-valued and so the condition is if that property has what the value specified then alert with a message saying hello.. The other idea is kinda complex that is to check using RegExp...

Here is the code..

         var array = new Array();
         array.fruit= "banana, apple";
         array.vegies ="cucumber, carrot, tomatos, potatos";
         array.drinks = "coke, pepsi";

         var pep = "pepsi";
         var getVal;
         outerloop:
         for(var obj in array){
              getVal = array[obj];
                println(obj + " : " + getVal);

                if ( obj.hasOwnProperty( "pepsi") ){        here is the problem
                     alert("hello");
                     break outerloop;
                }

         }

Cheers,

Dani AI

Generated

The root problem is that the loop variable is the property name (a string), not the property's contents — calling hasOwnProperty on that string won't find the item. , and are on the right track: you need to test the property's contents for membership (or store values as actual collections). A few practical, reliable options and gotchas not shown above:

  • If you need exact-token matching (avoid partial hits), use a RegExp that matches token boundaries and escape the search term first. Escaping avoids accidental special-character interpretation; see MDN on escaping in regular expressions for details (Regular Expressions — escaping).
function escapeRegExp(s) {
  return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

// build a pattern that requires the needle to be surrounded by start/end, comma or whitespace
var re = new RegExp('(^|\\s|,)' + escapeRegExp(needle) + '(\\s|,|$)', 'i');
if (re.test(propertyValue)) { alert('hello'); }
  • If you can change how values are stored, keep them as arrays or Sets. That makes membership tests simple and fast with Array.prototype.includes or a Set lookup. For scanning multiple properties, Object.values() + Array.prototype.some() is concise and expressive; both are described on MDN (Object.values, Array.prototype.some).

  • Beware trimming and case: split/normalize tokens and toLowerCase() if the input may include spaces or mixed case. Also avoid for...in for array iteration (it enumerates keys and inherited props); prefer for...of, .some(), or indices for arrays (for...in vs for...of).

These approaches cover quick one-off checks (regex), clean data design (arrays/Sets), and safe scanning across object properties.

Recommended Answers

All 3 Replies

I think you want if( getVal.indexOf("pepsi") != -1). Note that this also returns true if the given string looks like "FOO,BARpepsiBLA,BAZ". If you want to avoid that you can either split the string or use arrays instead of strings in the first place.

PS: There's no need for loop labels here as you only have one loop.

looking at the code I guess the "coke, pepsi" is not a property of array.drinks object. It's a value of the array.drinks property.

If you want to get pepsi or coke, you can try sepp2k suggesstion of using a split method for strings.

I think you're missunderstaing somethings...
First, you are not using the array as an array, but as an simple object(var array = new Object() would be prettier).
Second, pepsi is not a property of "array.drinks", it's the value.
What I think you want:

var array = new Object();

array.fruit= ["banana", "apple"];
array.vegies = ["cucumber", "carrot", "tomatos", "potatos"];
array.drinks = ["coke", "pepsi"];

outerloop:
for(var prop in array){
    var obj = array[prop];

    for(var index in obj) {
        var val = obj[index];
        if ( val == "pepsi" ){
            alert(val + " found");
            break outerloop;
        }
    }
 }

Hope it helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.