Best way to store a key=>value array in JavaScript?

前端 未结 7 1867
借酒劲吻你
借酒劲吻你 2020-11-28 00:27

What\'s the best way to store a key=>value array in javascript, and how can that be looped through?

The key of each element should be a tag, such as

7条回答
  •  温柔的废话
    2020-11-28 01:12

    In javascript a key value array is stored as an object. There are such things as arrays in javascript, but they are also somewhat considered objects still, check this guys answer - Why can I add named properties to an array as if it were an object?

    Arrays are typically seen using square bracket syntax, and objects ("key=>value" arrays) using curly bracket syntax, though you can access and set object properties using square bracket syntax as Alexey Romanov has shown.

    Arrays in javascript are typically used only with numeric, auto incremented keys, but javascript objects can hold named key value pairs, functions and even other objects as well.

    Simple Array eg.

    $(document).ready(function(){
    
        var countries = ['Canada','Us','France','Italy'];
        console.log('I am from '+countries[0]);
        $.each(countries, function(key, value) {
            console.log(key, value);
        });
    
    });
    

    Output -

    0 "Canada"

    1 "Us"

    2 "France"

    3 "Italy"

    We see above that we can loop a numerical array using the jQuery.each function and access info outside of the loop using square brackets with numerical keys.

    Simple Object (json)

    $(document).ready(function(){
    
        var person = {
            name: "James",
            occupation: "programmer",
            height: {
                feet: 6,
                inches: 1
            },
        }
    
        console.log("My name is "+person.name+" and I am a "+person.height.feet+" ft "+person.height.inches+" "+person.occupation);
    
        $.each(person, function(key, value) {
            console.log(key, value);
        });
    
    });
    

    Output -

    My name is James and I am a 6 ft 1 programmer

    name James

    occupation programmer

    height Object {feet: 6, inches: 1}

    In a language like php this would be considered a multidimensional array with key value pairs, or an array within an array. I'm assuming because you asked about how to loop through a key value array you would want to know how to get an object (key=>value array) like the person object above to have, let's say, more than one person.

    Well, now that we know javascript arrays are used typically for numeric indexing and objects more flexibly for associative indexing, we will use them together to create an array of objects that we can loop through, like so -

    JSON array (array of objects) -

    $(document).ready(function(){
    
        var people = [
            {
                name: "James",
                occupation: "programmer",
                height: {
                    feet: 6,
                    inches: 1
                }
            }, {
                name: "Peter",
                occupation: "designer",
                height: {
                    feet: 4,
                    inches: 10
                }
            }, {
                name: "Joshua",
                occupation: "CEO",
                height: {
                    feet: 5,
                    inches: 11
                }
            }
        ];
    
        console.log("My name is "+people[2].name+" and I am a "+people[2].height.feet+" ft "+people[2].height.inches+" "+people[2].occupation+"\n");
    
        $.each(people, function(key, person) {
            console.log("My name is "+person.name+" and I am a "+person.height.feet+" ft "+person.height.inches+" "+person.occupation+"\n");
        });
    
    });
    

    Output -

    My name is Joshua and I am a 5 ft 11 CEO

    My name is James and I am a 6 ft 1 programmer

    My name is Peter and I am a 4 ft 10 designer

    My name is Joshua and I am a 5 ft 11 CEO

    Note that outside the loop I have to use the square bracket syntax with a numeric key because this is now an numerically indexed array of objects, and of course inside the loop the numeric key is implied.

提交回复
热议问题