繁体   English   中英

如何将对象更改为数组以在sails.js 中创建多条记录?

[英]How to change object into array to create mutil record in sails.js?

我正在尝试使用 1.2.3 版的风帆创建多条记录

带输入:

{
    "selectedDog": [5,9,12] // dog id
    "checkInTime": "12h",
    "checkInDate": "2012-12-12",
    "nameOfService": "Hotel",
    "typeOfService": "Luxury"
}

我如何在每次预订时创建多条记录都有这样的信息

{

"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"

}

我的宠物模型:

bla bla

booking: {
      collection: 'booking',
      via: 'pets'
    },

我的预订模式:

bla bla

pets: {
      model: 'pets'
    }

我尝试使用 .createEach() 但我需要像这样将输入更改为数组:

[{
  pets: 5,
  "checkInTime": "12h",
    "checkInDate": "2012-12-12",
    "nameOfService": "Hotel",
    "typeOfService": "Luxury"
},
{..},
{..}
]

我不知道如何。 请注意,可以更多地选择狗或更少

有人有解决方案吗?

所以基本上我必须使用.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM