[英]How to change object into array to create mutil record in sails.js?
i'm trying to create a mutil records with sails ver 1.2.3我正在尝试使用 1.2.3 版的风帆创建多条记录
with input:带输入:
{
"selectedDog": [5,9,12] // dog id
"checkInTime": "12h",
"checkInDate": "2012-12-12",
"nameOfService": "Hotel",
"typeOfService": "Luxury"
}
How can i create mutil records with every booking have information like this我如何在每次预订时创建多条记录都有这样的信息
{
"pets": 5 // dog id
"checkInTime": "12h",
"checkInDate": "2012-12-12",
"nameOfService": "Hotel",
"typeOfService": "Luxury"
},
{
"pets": 9 // dog id
"checkInTime": "12h",
"checkInDate": "2012-12-12",
"nameOfService": "Hotel",
"typeOfService": "Luxury"
},{
"pets": 12 // dog id
"checkInTime": "12h",
"checkInDate": "2012-12-12",
"nameOfService": "Hotel",
"typeOfService": "Luxury"
}
My pet model:我的宠物模型:
bla bla
booking: {
collection: 'booking',
via: 'pets'
},
my booking model:我的预订模式:
bla bla
pets: {
model: 'pets'
}
i tried to use .createEach() but i need to change input into array like this:我尝试使用 .createEach() 但我需要像这样将输入更改为数组:
[{
pets: 5,
"checkInTime": "12h",
"checkInDate": "2012-12-12",
"nameOfService": "Hotel",
"typeOfService": "Luxury"
},
{..},
{..}
]
and i dont know how.我不知道如何。 Notice that could be more selectedDog or less
请注意,可以更多地选择狗或更少
anyone have a solution?有人有解决方案吗?
So basically i have to use .forEach()
and a loop here is my answer所以基本上我必须使用
.forEach()
并且这里的循环是我的答案
infor.forEach(dog => {
for (let id of dog.selectedDog) {
data = {
checkInTime: dog.checkInTime,
checkInDate: dog.checkInDate,
nameOfService: dog.nameOfService,
typeOfService: dog.typeOfService,
pets: id
};
array.push(data);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.