如何将一个新值添加到一个数值数组的结尾? [英] How to add a new value to the end of an numerical array?

查看:81
本文介绍了如何将一个新值添加到一个数值数组的结尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中我可以这样做:

In PHP I can do like:

$arrayname[] = 'value';

然后该值将被放置在最后一个数字键如。 4,如果3个按键已经存在。

Then the value will be put in the last numerical key eg. 4 if the 3 keys already exists.

在JavaScript的,我不能做同样的:

In JavaScript I can’t do the same with:

arrayname[] = 'value';

你是怎么做到这在JavaScript中?

How do you do it in JavaScript?

推荐答案

您可以使用方法。

结果
比如(使用Firebug快速测试)的:

首先,声明包含几个项目的数组:

First, declare an array that contains a couple of items :

>>> var a = [10, 20, 30, 'glop'];

该数组包含:

>>> a
[10, 20, 30, "glop"]

结果
而现在,一个新值到结束:

>>> a.push('test');
5

该数组现在包含:

The array now contains :

>>> a
[10, 20, 30, "glop", "test"]

这篇关于如何将一个新值添加到一个数值数组的结尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆