Solved

Determine if value/index exists in an array?

Posted on 2014-04-10
3
162 Views
Last Modified: 2014-04-10
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
Comment
Question by:894359
  • 2
3 Comments
 
LVL 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 500 total points
ID: 39993370
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
 

Author Comment

by:894359
ID: 39993374
I'm doing it as a safety measure as I might be reading other sources for data.
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39993383
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now