[英]Weird behavior of javascript
Please see attached screenshot. 请参阅附件的屏幕截图。 See pendingApp property of Object.
请参见Object的未决应用属性。 when I am debugging in eclipse then pendingApp show array of object, Which is correct!
当我在eclipse中调试时,pendingApp显示对象数组,这是正确的! But when I am
JSON.stringify(object)
then showing me empty Array. 但是当我是
JSON.stringify(object)
向我显示空数组。
Please let me know reason of this behavior. 请让我知道此行为的原因。 I think I am not aware with any Java-Script thought/concept ??
我想我不知道任何Java脚本的想法/概念? :P :)
:P :)
When I will save this Object into DB then blank array of pendingApp will be stored !! 当我将这个对象保存到数据库中时,将存储未决应用的空白数组!
var pending_app = [];
var new_record = {"pendingApp" : [], "installedApp" :[] };
....SOME CODE+conditions HERE....
pending_app[appId] = {'action' : action };
new_record.pendingApp = pending_app;
// create app-config data
return app_model.create(new_record); //will return promise object
It's not a weird behaviour but a common mistake of using an Array
to store key-value data. 这不是怪异的行为,而是使用
Array
存储键值数据的常见错误。
Short Answer : Use a literal Object to store these data 简短答案:使用文字对象存储这些数据
While you can add properties on every objects in Javascript, you cannot iterate over them with the default array mechanisms 虽然可以在Javascript中的每个对象上添加属性,但是无法使用默认的数组机制对其进行迭代
for (var i = 0; i < array.length; i++){}
array.forEach();
Simple demonstration : 简单演示:
var array = [];
array["anId"] = 1;
array.length; // 0
array[4294967295] = 1; // Indice >= unsigned 32-bit Max Value
array.length; // 0
array[4294967295]; // 1
So JSON.stringify
with the ECMAScript 5 Specification will use the Array
mechanism to iterate over all items and will find nothing. 因此,带有ECMAScript 5规范的
JSON.stringify
将使用Array
机制遍历所有项目,但一无所获。
Unlike Objects
that you can list properties with 与
Objects
不同,您可以使用列出Objects
Object.keys(array); // ["anId"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.