-2

I have a question about array as below

array = {}

and

array = [];

its same or not?.

also wish to ask

array { ID1 : "apple", ID2 : "Orange"}

and

array [ID1 : "apple", ID2 : "Orange"];

which is correct?.

now i coding my code with below and need your help to teach me about ARRAY.

var Store = [ store = 0, store1 = 0, store2 = 0,store3 = 0,store4 = 0];
var stock1 = 77,stock2 = 47,stock3 = 37,stock4 = 27,stock5 = 17;

for(i=0;i<stock1;i++){
store[0]++
}
var Sum_1 = Store;
document.getElementById('show1').innerHTML=Sum_1;

output will be

77

my question is how to i output it become

store = 77
store1 = 47
store2 = 37

Most with ID or Name together and value.

Thank you so much.

1
  • 1
    Just start reading: arrays and objects. Commented Feb 12, 2013 at 13:48

1 Answer 1

1

[] are used for a literal Array declaration, {} for a literal Object declaration.

Arrays are initialized without a value by using:

var my_array = [];

and with values:

var my_array = [1, 2, 3];

Note that you cannot set the index by this. You would have to do:

var my_array = [];
my_array["one"] = 1;
// etc.

You can then get "1" back by my_array["one"]

If you want to get the actual index name or "key" then you will need to do some trickery:

var outputString = "";

function ListAll()
{
    for(var key in my_array)
    {
        if(my_array.hasOwnProperty(key)) // Check if the key is actually a property (index)
        {
            outputString += key + " = " + my_array[key]; // add to string
        }
    }

    alert(outputString); // or assign the string to a div, whatever you need
}

in this example, key would be the index and my_array[key] the actual value

4
  • Mean i can't output my array with Name or ID together with value in that ID or Name?. Commented Feb 12, 2013 at 14:24
  • I just gave you a function that does exactly that. ID = index = key Commented Feb 12, 2013 at 14:25
  • So sorry, can you please write me a simple array which can diplay Name or Id after collect value from other. Commented Feb 12, 2013 at 14:30
  • Make this easy to read, i have try your code and found very usefull, however its still something wrong. It output with 0 = value, 1 = value. can it back to the name of array = [store , store1] store = value , store1 = value can this be done?? Commented Feb 12, 2013 at 14:42

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.