I have an existing Javascript Array with Keys, like:
我有一个现有的带有键的Javascript数组,比如:
var myArray = new Array();
myArray.push({ "id":"A123", "pwd":"helloworld", "items":[] });
So the ..
所以. .
myArray["items"] <-------- will store multi-dimension Arrays inside again.
.. is currently a blank room.
Then now, how do I add new multiple Arrays into this myArray["items]
room?
. .目前是空房间。现在,如何在myArray["items]空间中添加新的多个数组?
Lets say I have a loop to add items (total count is dynamic then):
假设我有一个添加项的循环(那么total count是动态的):
foreach(.......)
{
var newItem = [{"itemcode": "i1001", "itemname": "apple"}];
myArray.items.push( newItem ); // NOT WORKING
myArray["items"] = newItem; // NOT WORKING ALSO
}
Simply debug like this:
简单的调试:
alert( JSON.stringify( myArray ) );
.. and it is returning the Array Structure but the "items"
room is blank.
. .它返回数组结构,但是“items”空间是空的。
So how do I dynamically add new Objects into an existing Array room, with KEY, please?
那么如何动态地将新对象添加到现有的数组空间中呢?
1 个解决方案
#1
4
The problem is myArray
is an array, and the object which has items
array is at index 0 so
问题是myArray是一个数组,而具有项数组的对象是索引0
myArray[0].items.push( newItem );
In your case there is no need to use an array, just use a object lik
在您的情况下,不需要使用数组,只需使用对象lik。
var obj = {
"id": "A123",
"pwd": "helloworld",
"items": []
};
obj.items.push(obj);
#1
4
The problem is myArray
is an array, and the object which has items
array is at index 0 so
问题是myArray是一个数组,而具有项数组的对象是索引0
myArray[0].items.push( newItem );
In your case there is no need to use an array, just use a object lik
在您的情况下,不需要使用数组,只需使用对象lik。
var obj = {
"id": "A123",
"pwd": "helloworld",
"items": []
};
obj.items.push(obj);