0

How do you reference the name of an array within an array?

var jon_count = [0,6,7,9]
var sue_count = [9,7,6,8]
var rob_count = [7,8,6,3]
var name_list = {jon_count, sue_count, rob_count}

I'm trying to get the name of each variable within the "name_list" not the values of each item.

$.each(name_list, function (index, value) {
    $.each(value, function(ind, obj) {
       console.log(value[ind]);
    });
});

I know that's garbage, I want to see:

jon_count
sue_count
rob_count

But I keep getting the numbers.

2 Answers 2

1

You can just use the index variable:

$.each(name_list, function (index, value) {
  alert(index);
});

Here is a fiddle.

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

2 Comments

Thanks Steve. This worked. Is there a way to use this information to dynamically reference this, let's say in a loop like where I only have the first name tmname = index.toLowerCase(); window[tmname + "_count"][0] the problem here is that I'm getting an error on the index of the 0.
Oh, I found it. I have to reference the the first array and index that within the index (if that make sense) - I used: name_list[ thename ][0].
0

i think you need this:

var name_list = {jon_count:"item 1", sue_count:"item 2", rob_count:"item 3"} 
var keys = Object.keys(name_list)
keys.forEach((item) => {
  console.log(item)
})

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.