JavaScript: Adding to an associative array

53,931

Solution 1

Something like this should do the trick:

var arr = [];
function insert(name, number) {
    arr.push({
        name: name,
        number: number
    });        
}

Solution 2

I would use something like this;

var contacts = [];
var addContact = function(name, phone) {
    contacts.push({ name: name, phone: phone });
};

// Usage
addContact('John', '999');
addContact('Adam', '5433');

I don’t think you should try to parse the phone number as an integer as it could contain white-spaces, plus signs (+) and maybe even start with a zero (0).

Solution 3

var users = [];

users.push({name: "John", number: "999"});
users.push({name: "Adam", number: "5433"});

Solution 4

If you want you can add your function to Array.prototype.

Array.prototype.insert = function( key, val ) {
    var obj = {};
    obj[ key ] = val;
    this.push( obj );
    return this;
};

And use it like this.

var my_array = [].insert("John", "999")
                 .insert("Adam", "5433")
                 .insert("yowza", "1");

[
   0: {"John":"999"},
   1: {"Adam":"5433"},
   2: {"yowza":"1"}
]

Solution 5

I will assume you're using some array reference with insert:

var arr;
function insert(na, nu) {
  nu = Number(nu) || 0;
  //alternatively
  nu = parseInt(nu, 10);
  arr.push({ name: na, number: nu });
}
arr = [];


insert("John", "999");
insert("Adam", "5433");
Share:
53,931
ritch
Author by

ritch

Updated on July 11, 2020

Comments

  • ritch
    ritch almost 4 years

    I have a function called insert which takes two parameters (name and telnumber).

    When I call this function I want to add to an associative array.

    So for example, when I do the following:

    insert("John", "999");
    insert("Adam", "5433");
    

    I want to it so be stored like this:

    [0]
    {
    name: John, number: 999
    }
    [1]
    {
    name: Adam, number: 5433
    }
    
  • Adam Rackis
    Adam Rackis over 12 years
    You might point out that OP is not using an associative array; he's using an array of objects.
  • Leon
    Leon over 12 years
    eh? here's another one from MS
  • ioseb
    ioseb over 12 years
    "objects as associative arrays" doesn't mean that JS has "associative arrays". everything is an object in JS and you can add properties to anything but it doesn't make these objects associative arrays. btw who downvoted my answer? :)))))))))) it's funny :D
  • jabclab
    jabclab over 12 years
    Good point about the parseInt, I just saw "Number"- didn't think about the context :-)
  • jabclab
    jabclab over 12 years
    Just spotted, the contacts var above should be [] not {}.
  • gbtimmon
    gbtimmon over 11 years
    Technically any turning complete language can implement anything any other turing complete language. Thus all computer languages have associative arrays (minus ones like SQL). Also in JS, objects are implemented using lists of name/attribute pairs -- this is possible in JS and not other OO languages like Java since JS treats functions as first class citizens, so in fact they are "associative arrays as objects" not the other way around. and javascript does have them.
  • ioseb
    ioseb over 11 years
    love all these comments... do you guys write these comments with serious faces? :)))) thumbs up to down voters. did you try to find "associative array" definition in EcmaScript document?
  • gbtimmon
    gbtimmon over 11 years
    The problem is that assciative array is not a 'thing' it is a particular behavior of an array. Javascript has arrays that can be addresses associativly. How javascript choses to implement that or what it chosew to call it is javascript's choice but java script still has it. Saying javascript doesnt have associative arrays is like saying perl doesnt have functions because it uses 'subroutines'.