17

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
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ritch
  • 1,760
  • 14
  • 37
  • 65

6 Answers6

37

Something like this should do the trick:

var arr = [];
function insert(name, number) {
    arr.push({
        name: name,
        number: number
    });        
}
jabclab
  • 14,786
  • 5
  • 54
  • 51
5

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).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Stefan
  • 5,644
  • 4
  • 24
  • 31
4
var users = [];

users.push({name: "John", number: "999"});
users.push({name: "Adam", number: "5433"});
Tomalak
  • 332,285
  • 67
  • 532
  • 628
2

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"}
]
shredder
  • 381
  • 2
  • 3
1

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");
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
-2

There is no such term as an "associative array" in JavaScript, though you can use following:

var list = [];

function insert(name, number) {
  list.push({
    name: name,
    number: number
  });
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ioseb
  • 16,625
  • 3
  • 33
  • 29
  • eh? here's [another one from MS](http://msdn.microsoft.com/en-us/library/windows/apps/5kh4af6c(v=vs.85).aspx) – Leon Nov 30 '11 at 15:41
  • 2
    "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 – ioseb Nov 30 '11 at 15:44
  • 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. – gbtimmon Nov 02 '12 at 16:02
  • 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? – ioseb Nov 02 '12 at 20:21
  • 1
    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'. – gbtimmon Nov 05 '12 at 14:18