JavaScript Why are some values in my array undefined remove some values from array php,find some values in

This is because you are adding extra elements to an array. Take this code, for instance:

var a = []; // empty array
a[1] = 'foo'; // don't set a[0]

console.log(a.length); // gives 2

Javascript will always "fill in" gaps in the list of array keys. If you miss some out, Javascript will fill in the blanks with undefined.

Rather than adding the element to your array by the key name, just push it onto the end of the array:

imageArray.push(cboxes[i].value);
Answer:1

You get undefined because you're skipping indexes in imageArray. If the first checkbox isn't checked, it won't put anything in index 0, then because the second checkbox is checked, the first entry is placed into index 1.

When you iterate it doesn't skip those missed indexes if there is an index with a value after it, they just don't have a set value so it comes back as undefined.

You could change this line:

imageArray[i] = cboxes[i].value;

to:

imageArray.push(cboxes[i].value);

That way it won't skip indexes when there are unchecked checkboxes.

Answer:2

It's because you're setting the value of imageArray[i] only when the corresponding checkbox is checked. If you check checkboxes 2, 4 and 6, you're essentially doing this:

imageArray[1] = cboxes[1].value;
imageArray[3] = cboxes[3].value;
imageArray[5] = cboxes[5].value;

imageArray[0], [2] and [4] are never being set, and thus are undefined.

To fix this, either make use of push() to push the values into the imageArray, or simply set dummy values for non-matching keys:

    for (var i = 0; i < len; i++) {
        if (cboxes[i].checked) {
            imageArray[i] = cboxes[i].value;
        } else {
            imageArray[i] = "";
        }
    }

The result:

imageArray[0] = "";
imageArray[1] = cboxes[1].value;
imageArray[2] = "";
imageArray[3] = cboxes[3].value;
imageArray[4] = "";
imageArray[5] = cboxes[5].value;

Alternatively using push():

    for (var i = 0; i < len; i++) {
        if (cboxes[i].checked) {
            imageArray.push(cboxes[i].value);
        }
    }

The result:

imageArray[0] = cboxes[1].value;
imageArray[1] = cboxes[3].value;
imageArray[2] = cboxes[5].value;
Answer:3

try this

var cboxes = document.getElementsByName('checkbox[]');
var imageArray =[];

for (var i = 0, len = cboxes.length ; i < len; i++) {
    if (cboxes[i].checked) {
        imageArray.push(cboxes[i].value );
    } 
}
Answer:4

I wanted Two Google Maps API in single html file. SO I have done like following. But it is not working. Please help me by guiding why I cant and where I am mistaking? Thanks in advance. <html&...

I wanted Two Google Maps API in single html file. SO I have done like following. But it is not working. Please help me by guiding why I cant and where I am mistaking? Thanks in advance. <html&...

  1. longest possible google maps route
  2. longest possible drive google maps

I have to modify some code and the previous developer left this comment: color: color, // e.g. '#RRGGBBFF' - Last 2 digits are alpha information On the page there is a color picker that let's the ...

I have to modify some code and the previous developer left this comment: color: color, // e.g. '#RRGGBBFF' - Last 2 digits are alpha information On the page there is a color picker that let's the ...

<script type="text/javascript" src="/js/jquery1.8.3.js"></script> <script> $(document).ready(function(){ $("li").has("ul").bind("click",function(e){ console.log($(...

<script type="text/javascript" src="/js/jquery1.8.3.js"></script> <script> $(document).ready(function(){ $("li").has("ul").bind("click",function(e){ console.log($(...

Getting time zone when giving state and country in text box is there any way to get the time zone of that place with this two values? the input will be like var state = 'New York' ; var country = '...

Getting time zone when giving state and country in text box is there any way to get the time zone of that place with this two values? the input will be like var state = 'New York' ; var country = '...

  1. getting time zone in c#
  2. getting time zone
  3. getting used to time zone
  4. getting baby back in time zone