0

I have a JavaScript multi-dimensional array that had multiple values with a ton of sub-values. Here's what I mean:

var items = {
    "Area": [
        "Feet",
        "Meters"
    ],
    "Frequency": [
        "Hertz",
        "Kilohertz"
    ]
};

I want to cycle through each first dimension item (like Area and Frequency). I do not wanna cycle through sub-values but get the name of the first dimension values.

Here's my code to cycle:

$(function() {
    $.each(items, function(index) {
        $(".category").append("<option>" + items[index].text + "</option>");
    });
});

Just to be sure, I want to get the names of each first-dimension values, like Area and Frequency. I've tried things like .text, .val, .val() (jQuery), .innerHTML, etc but nothing seems to work.

EDIT: Putting nothing after items[index] just returns the sub-values to each first dimension items.

1
  • You want Object.keys(items) Commented Nov 19, 2016 at 4:02

2 Answers 2

3

What you have rather than a multi-dimensional array is an object (or map, hashmap, whatever it's called in other languages ;))

To get all the keys you can use Object.keys(myObj);

var items = {
    "Area": [
        "Feet",
        "Meters"
    ],
    "Frequency": [
        "Hertz",
        "Kilohertz"
    ]
};

var keys = Object.keys(items);
console.log(keys);

// iterate through them
keys.forEach(function(item, index) {
  console.log(index, item);
});

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

1 Comment

Thanks for the quick reply, but Aniket Sahrawat's post helped me and is a lot simpler so I'm gonna stick with his.Thank you anyways and I hope this helps someone else with their issues.
1

When you do items[index], it means you are referencing to value of items. index is the key and items[index] is the value. Try this code:

var items = {
  "Area": [
    "Feet",
    "Meters"
  ],
  "Frequency": [
    "Hertz",
    "Kilohertz"
  ]
};
$(function() {
  $.each(items, function(index, value) {
    $(".category").append("<option>" + index + "</option>");
    console.log(index);
    console.log(value);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

4 Comments

Thank you! At first I thought that wouldn't work because usually "index" is the number of the item that is current cycled through, but it worked! I'm gonna use this from now on.
index is the key and not a number
@JacobGunther also this:- if you are doing $.each(items, function(index), then the word index is actually a value. If you want key and values you must do like $.each(items, function(k, v) where k is the key/index and v is value corresponding to the key/index.
Thanks for that, I kept thinking index was the number that was currently being cycled, but I forgot about value. I'm very used to PHP and that is probably why I got them mixed up.

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.