如何字符串化在Javascript数组? [英] How to stringify an array in Javascript?

查看:96
本文介绍了如何字符串化在Javascript数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行下面的件code 的:

var arr = [];

arr["aaa"] = {
    "xxx" : 1,
    "ttt" : 2
};

arr["bbb"] = {
    "xxx" : 5,
    "qqq" : 6
};

var tmp = JSON.stringify(arr);

alert(tmp);

但结果是 [] 。怎样才能用字符串化字符串键和对象值的数组?

But the result is []. How does one stringify an array with string keys and object values?

推荐答案

使用

var arr = {};

阵列应只用于数字索引的数据,而不是任意属性。当然,你可以做到这一点,因为他们是,事实上,对象。但它不会在JSON工作。

Arrays should only be used for numerically indexed data, not arbitrary properties. Of course you are able to do it, because they are, in fact, objects. But it won't work in JSON.

所以,应该使用对象。

这篇关于如何字符串化在Javascript数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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