0

Okay, so I have an array of strings on a webpage in Javascript, defined like so:

var arr = ["Apple", "Orange", "Pear"]; //this is populated by a php script prior to being sent to the client.

The problem is, when I attempt to access these items in a for loop like this:

for (var i = 0; i < arr.length; i++)
{
    alert(arr[i]);
}

My output would be:

A
p
p

So, the array seems to be accessed one character at a time, which is of course not the reason I created the array.

I really cannot fathom why this would happen. Can anyone see a problem?

EDIT: The full code - it's not very nice but it's a work in progress:

var votes = {<?php foreach ($options as $o) { echo ' "'.$o['name'].'":"'.$o['votes'].'"'; if (next($options)) { echo ','; } } ?>};
var name = [<?php for ($i = 0; $i < count($options); $i++) { echo ' "'.$options[$i]['name'].'"'; if ($i < count($options)-1) { echo ','; } } ?> ];

function run(num) {
document.getElementById('out').innerHTML = '';


for (var i = 0; i < num; i++) {
    if (document.getElementById(i+1).checked)
    {
        votes[name[i]] = (parseInt(votes[name[i]]) + 1);
        document.getElementById(num+(i+1)).checked = true;
    }
    if (document.getElementById(num+(i+1)).checked)
    {
        votes[name[i]] = (parseInt(votes[name[i]]) + 1);
    }

    document.getElementById('out').innerHTML += name[i] + ': ' + votes[name[i]] + '<br />';
    console.log('arr: ', name, 'j: ', name.length);
}

$.ajax("vote.php", {
        data:votes,
            });
}

Here is Options

"options" : [
    {
        "name"  : "Children of Men",
        "id"    : "1",
        "votes" : "0"
    },
    {
        "name"  : "City of God",
        "id"    : "2",
        "votes" : "0"
    },
    {
        "name"  : "Hidden",
        "id"    : "3",
        "votes" : "0"
    },
    {
        "name"  : "We Need To Talk About Kevin",
        "id"    : "4",
        "votes" : "0"
    }
]
12
  • 4
    That code won't do what you say it's doing, so something must be wrong with the way you're getting the array from your server to the browser. Commented Jan 28, 2013 at 17:26
  • 1
    It sounds like you reassigned arr to be one element of the original arr array. Commented Jan 28, 2013 at 17:26
  • 1
    Works here -- jsfiddle.net/DFpW3 Commented Jan 28, 2013 at 17:27
  • 1
    @Luke: And what is alert(typeof arr)? Sounds like it was a string somehow Commented Jan 28, 2013 at 17:40
  • 3
    @Luke: Not with the above code. Could you please show us your actual code in whole (thanks for the PHP part), what ends up in the source of the page? Commented Jan 28, 2013 at 17:46

2 Answers 2

1
var arr = ['Apple', 'Orange', 'Pear'],
    i,
    j = arr.length;

console.log('Array?: ', arr.toString() == '[object Array]'); // edited

for (i = 0; i < j; i++) {
    console.log('arr: ', arr, 'j: ', j);
}

Above console.log should show you what is wrong.

Sign up to request clarification or add additional context in comments.

4 Comments

in my console.log arr is the contents of the array separated by commas, and j is 62... Which is the number of characters in the 'arr' output. Really cannot figure out why it is doing that.
@Luke OK, what about toString.call(arr) == '[object Array]'; is it true?
@Luke OK, so you have a string not an array. The problem is in your php code which makes not an array but a string.
@Luke Somehow you have structure like this: 'Apple,Orange,Pear' but you need it to be like this: ['Apple','Orange','Pear']
0

In the end I "fixed" this problem by defining the Array with

new Array( 'Apple', 'Orange', 'Pear' );

I could not figure out the actual issue, unfortunately.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.