JavaScript推送到数组 [英] JavaScript push to array

查看:87
本文介绍了JavaScript推送到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将新值推送到以下数组?

How do I push new values to the following array?

json = {"cool":"34.33","alsocool":"45454"}

我尝试了json.push("coolness":"34.33");,但是没有用.

I tried json.push("coolness":"34.33");, but it didn't work.

推荐答案

它不是数组.

var json = {"cool":"34.33","alsocool":"45454"};
json.coolness = 34.33;

var json = {"cool":"34.33","alsocool":"45454"};
json['coolness'] = 34.33;

您可以将其作为数组来执行,但这将是一种不同的语法(这几乎肯定不是您想要的)

you could do it as an array, but it would be a different syntax (and this is almost certainly not what you want)

var json = [{"cool":"34.33"},{"alsocool":"45454"}];
json.push({"coolness":"34.33"});

请注意,此变量名称极易引起误解,因为此处没有JSON .我会用别的名字命名.

Note that this variable name is highly misleading, as there is no JSON here. I would name it something else.

这篇关于JavaScript推送到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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