[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 182
  • Last Modified:

Determine if value/index exists in an array?

What is the best way to detect if I'm getting the values I expect in an array or if it is missing? for example;
var myArr = ['value0-0'];
var newValues = ['value1-0',1];
myArr.push(newValues);
newValues = ['value2-0',1];
myArr.push(newValues);
var newValues = ['value3-0',0];
myArr.push(newValues);
var newValues = ['value4-0'];
myArr.push(newValues);

Open in new window

This could go on in any order etc. But I want to check to ensure that there is a second value? Can I do something like:
for (var i = 0; i < myArr.length; i++) {
    if((typeof (myArr[i][1])) !== 'undefined'){
        ... then do something
    }
}

Open in new window

0
894359
Asked:
894359
  • 2
1 Solution
 
Scott Fell, EE MVEDeveloperCommented:
That looks fine but why not just create a function that checks for valid data

function addArr(newValues){
  if (newValues[0][1]!== 'undefined'){
      myArr.push(newValues);
  } 
}

Open in new window

0
 
894359Author Commented:
I'm doing it as a safety measure as I might be reading other sources for data.
0
 
Scott Fell, EE MVEDeveloperCommented:
Then I would go with the function.  And if you are doing anything with the input server side with your database, make sure you double check it there as the js function can easily be worked around.
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now