0

What data structure is this in JavaScript? Is seems like a list, but then why does it have a curly brace and not the standard "[]"?

    var states = {
      "California": {
       "Monterey": ["Salinas", "Gonzales"],
       "Alameda": ["Oakland", "Berkeley"]
       },
    "Oregon": {
      "Douglas": ["Roseburg", "Winston"],
      "Jackson": ["Medford", "Jacksonville"]
      }
    }

I am interested in creating some sort of array that has a structure like this:

Colors: Red, Green, Blue, Orange, Yellow

Food: Bananas, Oranges

People: Me, You, Us, Them

...

Does something like this exist in Javascript?

Thanks!

1
  • It's an object with nested objects. Commented Aug 8, 2016 at 18:00

5 Answers 5

3

One of the main tools available to you in Javascript is objects. They're hash maps, or dictionaries, or key-value pairs, or whatever you'd like to call them. The key is a string (or symbol) and the value can be anything, including other objects.

To create the kind of relationship you're talking about, you could write something like this:

var obj = {
  colors: ['red', 'green', 'blue'],
  food: ['bananas', 'oranges'],
  people: ['me', 'you', 'us']
};
console.log(obj.colors);
console.log(obj.food);
console.log(obj.people);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the clear information and links, Kabali's answer is closer to what I was looking for since its easier to make dynamic values with. I appreciate the help :)
@noc_coder For what it's worth, Kabali's way of adding new properties to an object is unconventional and not recommended. Doing obj.Colors = innerArray is the same thing as obj['Colors'] = innerArray.
2

Answer of first part of your question is this is an object. We can declare object like that. for practice Object you can use this link.For the second part There are many way by which you can add elements to an object. In my example I declare an object an then add attributes in the object.

var obj = {};
  obj.Colors = ['Red', 'Green', 'Blue','Orange', 'Yellow'],
  obj.Food = ['Bananas', 'Oranges'],
  obj.People = ['Me', 'You', 'Us','Them'] 
  console.log(obj.Colors);
  console.log(obj.Food);
  console.log(obj.People);

Comments

1

This is an object. It can have as many nested data types as you like

2 Comments

But what if my properties are uneven and unrelated as in the example I gave above??
That's ok! JS objects have few rules - try it out!
1
<script>   
var arrayObj = [];

var obj = {};
var innerArray = ['Red', 'Green', 'Blue', 'Orange', 'Yellow'];
var secondArray = ['Me', 'You', 'Us', 'Them'];
obj['Colors'] = innerArray;
obj['People'] = secondArray ;
arrayObj .push(obj);
</script>

You can do like this

Comments

0

Should be an object with nested objects array. Somewhat similar with this.

var countries = {
    "USA" : {
         "Alabama" : {
                "Birmingham" : 371,
                "Montgomery" : 8771 },
         "Alaska" : {
                "Anchorage" : 9918,
                "Nome" : 8171 },
          "Wyoming"  : {
                "Casper" : 718,
                "Cody" : 888 }
        },
    "Canada" : [ 
        "Alberta" : {
              "Calgary" : "Z871" },
        "British Columbia" {
              "Vancouver" : "B88C",
              "Victoria" : "C99A" },
      ...
};

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.