Javascript:添加到关联数组

时间:2022-08-26 14:15:29

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

我有一个叫insert的函数,它有两个参数(name, 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
}

6 个解决方案

#1


27  

Something like this should do the trick:

像这样的东西应该可以达到这个目的:

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

#2


4  

var users = [];

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

#3


4  

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

´我不认为你应该尝试解析电话号码那样一个整数可以包含空格,加号(+),甚至从一个零(0)。

#4


2  

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

如果需要,可以将函数添加到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"}
]

#5


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");

#6


-1  

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

JS中不存在“关联数组”这样的术语,但您可以使用以下术语:

var list = [];

function insert(name, number) {
  list.push({
    name: name,
    number: number
  });
}

#1


27  

Something like this should do the trick:

像这样的东西应该可以达到这个目的:

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

#2


4  

var users = [];

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

#3


4  

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

´我不认为你应该尝试解析电话号码那样一个整数可以包含空格,加号(+),甚至从一个零(0)。

#4


2  

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

如果需要,可以将函数添加到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"}
]

#5


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");

#6


-1  

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

JS中不存在“关联数组”这样的术语,但您可以使用以下术语:

var list = [];

function insert(name, number) {
  list.push({
    name: name,
    number: number
  });
}

相关文章