**我想将多个对象存储到一个数组中,我有五个对象obj1,obj2,obj3,obj4,obj5 **
答案 0 :(得分:0)
好吧,您可以通过
来简单地初始化一个数组let someArray = [];
然后,您可以通过对每个对象使用传播运算符来添加对象,如下所示:
someArray = [...ObjectReference];
答案 1 :(得分:0)
我同意这过于广泛,而且细节不够。但是,这是在javascript中添加对象和数组的基本知识。
“添加”一个对象
let arr = [];
let obj = {
"by": "5d7bcc50ca80630017807ada",
"id": "5d7fd4884680ef00171cf3d4",
"message": "salaam",
"to": "5d7f39c6dacf250017ccac85"
};
arr.push(obj);
OR
let obj = {
"by": "5d7bcc50ca80630017807ada",
"id": "5d7fd4884680ef00171cf3d4",
"message": "salaam",
"to": "5d7f39c6dacf250017ccac85"
};
let arr = [obj];
在发布的代码中,这取决于您如何接收/构建这些对象,但这是一个非常简单的示例。
let obj1 = {
"by": "5d7bcc50ca80630017807ada",
"id": "5d7fd4884680ef00171cf3d4",
"message": "salaam",
"to": "5d7f39c6dacf250017ccac85",
};
let obj2 = {
"by": "5d7bcc50ca80630017807ada",
"id": "5d7fd4884680ef00171cf3d4",
"message": "salaam",
"to": "5d7f39c6dacf250017ccac85",
};
let obj3 = {
"by": "5d7bcc50ca80630017807ada",
"id": "5d7fd4884680ef00171cf3d4",
"message": "salaam",
"to": "5d7f39c6dacf250017ccac85",
};
let obj4 = {
"by": "5d7bcc50ca80630017807ada",
"id": "5d7fd4884680ef00171cf3d4",
"message": "salaam",
"to": "5d7f39c6dacf250017ccac85",
};
let arr = [obj1, obj2, obj3, obj4];
OR
let arr = [];
arr.push(obj1);
arr.push(obj2);
arr.push(obj3);
arr.push(obj4);
在VueJS组件中,您可以在数据对象内设置一个键:
data () {
return {
arr: []
}
}
您要执行的操作:
this.arr.push(obj);
或
this.arr = [obj];
两者都是反应性的https://vuejs.org/v2/guide/list.html#Array-Change-Detection
对于那些遇到这种情况的人,我特意省略了更复杂的示例或扩展运算符语法。基于这个问题,我的假设是他们才刚刚起步,不想让他们不知所措。