在 JavaScript 中向现有数组添加新值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1996747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:17:43  来源:igfitidea点击:

Add new value to an existing array in JavaScript

javascriptarrays

提问by Andrew

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 中做同样的事情?

回答by David Hellsing

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.ex 一样访问值obj[0]

回答by Mike

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()出于以下原因:

  • new Array(1, 2)is equivalent to [1, 2], but new Array(1)is notequivalent to [1]. Rather the latter is closer to [undefined], since a single integer argument to the Arrayconstructor indicates the desired array length.
  • Array, just like any other built-in JavaScript class, is not a keyword. Therefore, someone could easily define Arrayin your code to do something other than construct an array.
  • new Array(1, 2)相当于[1, 2],但是new Array(1)不是等同于[1]。相反,后者更接近[undefined],因为Array构造函数的单个整数参数表示所需的数组长度。
  • Array,就像任何其他内置 JavaScript 类一样,不是关键字。因此,有人可以轻松地Array在您的代码中定义除构造数组之外的其他操作。

回答by nemisj

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.

Array 是一个 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.

选择您需要的一种。

回答by nickf

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()是添加到数组的更好方法,因为您不需要知道已经存在多少项,并且您可以在一个函数调用中添加许多项。

回答by mopoke

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

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

e.g.

例如

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

回答by Sitepor500.com.br

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

回答by Antony Hatchkins

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

it's not so much jquery as javascript

它不像 javascript 那么多 jquery

回答by cllpse

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