我想将一个对象推入数组的内部数组。
我的数组:
[{"roomid":"652994","Listener":[],"broadcaster":[]}]
我想将以下对象推入Listener
数组中:
{"name":"jony","age":"26"}
成为:
{"roomid":"652994","Listener":[{"name":"jony","age":"26"},{"name":"pear","age":"32"}],"broadcaster":[]}
答案 0 :(得分:0)
使用数组的第一个索引访问属性Listener
,然后访问push()
对象:
arr[0].Listener.push(obj);
var arr = [{"roomid":"652994","Listener":[],"broadcaster":[]}]
var obj = {"name":"jony","age":"26"};
var obj2 = {"name":"pear","age":"32"};
arr[0].Listener.push(obj, obj2);
console.log(arr);
答案 1 :(得分:0)
只需将它推到想要的位置即可
var array = [{
"roomid": "652994",
"Listener": [],
"broadcaster": []
}]
var toPush1 = {
"name": "jony",
"age": "26"
}
var toPush2 = {
"name": "pear",
"age": "32"
}
array[0].Listener.push(toPush1);
array[0].Listener.push(toPush2);
console.log(array);
console.log({
"roomid": "652994",
"Listener": [{
"name": "jony",
"age": "26"
}, {
"name": "pear",
"age": "32"
}],
"broadcaster": []
});