将新值添加到JavaScript中的现有数组[复制]

时间:2023-01-29 13:51:40

This question already has an answer here:

这个问题已经有了答案:

In PHP, I'd do something like:

在PHP中,我要做以下事情:

$array = array();
$array[] = "value1";
$array[] = "value2";
$array[] = "value3";

How would I do the same thing in JavaScript?

如何在JavaScript中做同样的事情?

8 个解决方案

#1


356  

You don't need jQuery for that. Use regular javascript

你不需要jQuery。使用常规的javascript

var arr = new Array();
// or var arr = [];
arr.push('value1');
arr.push('value2');

Note: In javascript, you can also use Objects as Arrays, but still have access to the Array prototypes. This makes the object behave like an array:

注意:在javascript中,也可以将对象用作数组,但仍然可以访问数组原型。这使得对象的行为像一个数组:

var obj = new Object();
Array.prototype.push.call(obj, 'value');

will create an object that looks like:

将创建一个看起来像:

{
    0: 'value',
    length: 1
}

You can access the vaules just like a normal array f.ex obj[0].

可以像普通数组f那样访问这些vaules。obj[0]。

#2


101  

This has nothing to do with jQuery, just JavaScript in general.

这与jQuery没有关系,只是一般的JavaScript。

To create an array in JavaScript:

使用JavaScript创建数组:

var a = [];

Or:

或者:

var a = ['value1', 'value2', 'value3'];

To append values on the end of existing array:

将值附加到现有数组的末尾:

a.push('value4');

To create a new array, you should really use [] instead of new Array() for the following reasons:

要创建一个新的数组,您应该使用[]而不是新的数组(),原因如下:

  • new Array(1, 2) is equivalent to [1, 2], but new Array(1) is not equivalent to [1]. Rather the latter is closer to [undefined], since a single integer argument to the Array constructor indicates the desired array length.
  • 新数组(1,2)等价于[1,2],但新数组(1)不等价于[1]。相反,后者更接近[undefined],因为数组构造函数的单个整数参数指示所需的数组长度。
  • Array, just like any other built-in JavaScript class, is not a keyword. Therefore, someone could easily define Array in your code to do something other than construct an array.
  • 和其他内置的JavaScript类一样,数组不是关键字。因此,有人可以很容易地在代码中定义数组来做其他事情,而不是构造一个数组。

#3


32  

Array is a JavaScript native object, why don't you just try to use the API of it? Knowing API on its own will save you time when you will switch to pure JavaScript or another framework.

数组是一个JavaScript原生对象,为什么不尝试使用它的API呢?当您切换到纯JavaScript或其他框架时,了解API本身将节省您的时间。

There are number of different possibilities, so, use the one which mostly targets your needs.

有许多不同的可能性,所以,使用一个主要针对你的需求的。

Creating array with values:

创建数组的值:

var array = ["value1", "value2", "value3"];

Adding values to the end

在末尾添加值

var array = [];
array.push("value1");
array.push("value2");
array.push("value3");

Adding values to the begin:

在开始添加值:

var array = [];
array.unshift("value1");
array.unshift("value2");
array.unshift("value3");

Adding values at some index:

在某些指标上增加值:

var array = [];
array[index] = "value1";

or by using splice

或使用拼接

array.splice(index, 0, "value1", "value2", "value3");

Choose one you need.

你需要选择一个。

#4


11  

You can use the .push() method (which is standard JavaScript)

您可以使用.push()方法(这是标准的JavaScript)

e.g.

如。

var primates = new Array();
primates.push('monkey');
primates.push('chimp');

#5


10  

There are several ways:

有几种方法:

Instantiating the array:

实例化数组:

var arr;

arr = new Array(); // empty array

// ---

arr = [];          // empty array

// ---

arr = new Array(3);
alert(arr.length);  // 3
alert(arr[0]); // undefined

// ---

arr = [3];
alert(arr.length);  // 1
alert(arr[0]); // 3

Pushing to the array:

将数组:

arr = [3];     // arr == [3]
arr[1] = 4;    // arr == [3, 4]
arr[2] = 5;    // arr == [3, 4, 5]
arr[4] = 7;    // arr == [3, 4, 5, undefined, 7]

// ---

arr = [3];
arr.push(4);        // arr == [3, 4]
arr.push(5);        // arr == [3, 4, 5]
arr.push(6, 7, 8);  // arr == [3, 4, 5, 6, 7, 8]

Using .push() is the better way to add to an array, since you don't need to know how many items are already there, and you can add many items in one function call.

使用.push()是向数组添加的更好方法,因为您不需要知道已经有多少项,并且可以在一次函数调用中添加许多项。

#6


1  

array = ["value1", "value2", "value3"]

it's not so much jquery as javascript

与其说它是jquery,不如说是javascript

#7


1  

Indeed, you must initialize your array then right after that use array.push() command line.

实际上,您必须在使用array.push()命令行之后初始化数组。

var array = new Array();
array.push("first value");
array.push("second value");

#8


0  

jQuery is an abstraction of JavaScript. Think of jQuery as a sub-set of JavaScript, aimed at working with the DOM. That being said; there are functions for adding item(s) to a collection. I would use basic JavaScript in your case though:

jQuery是JavaScript的一种抽象。可以将jQuery看作是JavaScript的一组子集合,旨在处理DOM。也就是说;有向集合添加项的函数。在你的例子中我会使用基本的JavaScript:

var array;

array[0] = "value1";
array[1] = "value2";
array[2] = "value3";

... Or:

…或者:

var array = ["value1", "value2", "value3"];

... Or:

…或者:

var array = [];

array.push("value1");
array.push("value2");
array.push("value3");

#1


356  

You don't need jQuery for that. Use regular javascript

你不需要jQuery。使用常规的javascript

var arr = new Array();
// or var arr = [];
arr.push('value1');
arr.push('value2');

Note: In javascript, you can also use Objects as Arrays, but still have access to the Array prototypes. This makes the object behave like an array:

注意:在javascript中,也可以将对象用作数组,但仍然可以访问数组原型。这使得对象的行为像一个数组:

var obj = new Object();
Array.prototype.push.call(obj, 'value');

will create an object that looks like:

将创建一个看起来像:

{
    0: 'value',
    length: 1
}

You can access the vaules just like a normal array f.ex obj[0].

可以像普通数组f那样访问这些vaules。obj[0]。

#2


101  

This has nothing to do with jQuery, just JavaScript in general.

这与jQuery没有关系,只是一般的JavaScript。

To create an array in JavaScript:

使用JavaScript创建数组:

var a = [];

Or:

或者:

var a = ['value1', 'value2', 'value3'];

To append values on the end of existing array:

将值附加到现有数组的末尾:

a.push('value4');

To create a new array, you should really use [] instead of new Array() for the following reasons:

要创建一个新的数组,您应该使用[]而不是新的数组(),原因如下:

  • new Array(1, 2) is equivalent to [1, 2], but new Array(1) is not equivalent to [1]. Rather the latter is closer to [undefined], since a single integer argument to the Array constructor indicates the desired array length.
  • 新数组(1,2)等价于[1,2],但新数组(1)不等价于[1]。相反,后者更接近[undefined],因为数组构造函数的单个整数参数指示所需的数组长度。
  • Array, just like any other built-in JavaScript class, is not a keyword. Therefore, someone could easily define Array in your code to do something other than construct an array.
  • 和其他内置的JavaScript类一样,数组不是关键字。因此,有人可以很容易地在代码中定义数组来做其他事情,而不是构造一个数组。

#3


32  

Array is a JavaScript native object, why don't you just try to use the API of it? Knowing API on its own will save you time when you will switch to pure JavaScript or another framework.

数组是一个JavaScript原生对象,为什么不尝试使用它的API呢?当您切换到纯JavaScript或其他框架时,了解API本身将节省您的时间。

There are number of different possibilities, so, use the one which mostly targets your needs.

有许多不同的可能性,所以,使用一个主要针对你的需求的。

Creating array with values:

创建数组的值:

var array = ["value1", "value2", "value3"];

Adding values to the end

在末尾添加值

var array = [];
array.push("value1");
array.push("value2");
array.push("value3");

Adding values to the begin:

在开始添加值:

var array = [];
array.unshift("value1");
array.unshift("value2");
array.unshift("value3");

Adding values at some index:

在某些指标上增加值:

var array = [];
array[index] = "value1";

or by using splice

或使用拼接

array.splice(index, 0, "value1", "value2", "value3");

Choose one you need.

你需要选择一个。

#4


11  

You can use the .push() method (which is standard JavaScript)

您可以使用.push()方法(这是标准的JavaScript)

e.g.

如。

var primates = new Array();
primates.push('monkey');
primates.push('chimp');

#5


10  

There are several ways:

有几种方法:

Instantiating the array:

实例化数组:

var arr;

arr = new Array(); // empty array

// ---

arr = [];          // empty array

// ---

arr = new Array(3);
alert(arr.length);  // 3
alert(arr[0]); // undefined

// ---

arr = [3];
alert(arr.length);  // 1
alert(arr[0]); // 3

Pushing to the array:

将数组:

arr = [3];     // arr == [3]
arr[1] = 4;    // arr == [3, 4]
arr[2] = 5;    // arr == [3, 4, 5]
arr[4] = 7;    // arr == [3, 4, 5, undefined, 7]

// ---

arr = [3];
arr.push(4);        // arr == [3, 4]
arr.push(5);        // arr == [3, 4, 5]
arr.push(6, 7, 8);  // arr == [3, 4, 5, 6, 7, 8]

Using .push() is the better way to add to an array, since you don't need to know how many items are already there, and you can add many items in one function call.

使用.push()是向数组添加的更好方法,因为您不需要知道已经有多少项,并且可以在一次函数调用中添加许多项。

#6


1  

array = ["value1", "value2", "value3"]

it's not so much jquery as javascript

与其说它是jquery,不如说是javascript

#7


1  

Indeed, you must initialize your array then right after that use array.push() command line.

实际上,您必须在使用array.push()命令行之后初始化数组。

var array = new Array();
array.push("first value");
array.push("second value");

#8


0  

jQuery is an abstraction of JavaScript. Think of jQuery as a sub-set of JavaScript, aimed at working with the DOM. That being said; there are functions for adding item(s) to a collection. I would use basic JavaScript in your case though:

jQuery是JavaScript的一种抽象。可以将jQuery看作是JavaScript的一组子集合,旨在处理DOM。也就是说;有向集合添加项的函数。在你的例子中我会使用基本的JavaScript:

var array;

array[0] = "value1";
array[1] = "value2";
array[2] = "value3";

... Or:

…或者:

var array = ["value1", "value2", "value3"];

... Or:

…或者:

var array = [];

array.push("value1");
array.push("value2");
array.push("value3");