Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Determine if value/index exists in an array?

Posted on 2014-04-10
3
Medium Priority
?
177 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 54

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 54

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…

618 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