0

I have an object which contains a variable number of arrays. The property title always is a number (like here: 15, 117). I could simply access the arrays with names[15] or names[117], but those values are changing constantly because of a data-request.

How can I access them as "the first" or "the second"???

var names = {
    15: Array[1];
    117: Array[1];
};

If this isn't working, I tried a for...in loop to store the arrays in variables, but it didn't really work out:

var name1, name2;

for(var key in names){
    if(name1){name2 = names[key];}
    if(!name1){name1 = names[key];}
}

As soon as there are more arrays, it's overriding name1 with name2 and so on...

Any idea how to solve this? Thanks already for your time.

3
  • You can access them just with loop Commented Jul 26, 2013 at 10:32
  • What is Array supposed to be here? Do you mean just some sample array, or did you really mean to use the Array constructor? Commented Jul 26, 2013 at 13:30
  • It doesn't really matter. Its an array which contains again an object with different properties. I'm then accessing some properties with string values. Commented Jul 26, 2013 at 13:44

3 Answers 3

1

I deleted my earlier answer, as i think is not accurate. js fiddle

var names ={1: ["a","b"],2:["c","d"],3:["e","f"]}

var nameArr=[],i=0; for(var key in names){ nameArr[i++] = names[key]; }

for(i=0;i<nameArr.length;i++)
alert(nameArr[i]);
Sign up to request clarification or add additional context in comments.

Comments

0

There is example, how you can access to properties of your object with loop: http://jsfiddle.net/Y7mHB/

var names = {
    15: '15',
    117: '117'
};

for(var key in names) {
    alert(key + ' ' + names[key]);   
}

Comments

0

Yes that's absolutely true to access them like this. My problem is, I have to store them separately as variables, to then pass them to a function who could look like this:

var a = name[key];//the first object-property (how to store??)
var b = name[key];//the second object-property (how to store??) 

function doSomething(a,b){
//do something usefull
}

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.