简体   繁体   中英

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

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:

[{
  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

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);
      }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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