?
Solved

Determine if value/index exists in an array?

Posted on 2014-04-10
3
Medium Priority
?
176 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 53

Accepted Solution

by:
Scott Fell,  EE MVE earned 2000 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 53

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Suggested Courses

801 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