Hello Community, first time asking for support here in the Zapier Forum.
I’m looking for advice on how to output multiple objects from an array using JavaScript and the Code by Zapier app. So far I’ve been able to pull the data from the array’s string by using the `.split()` method and to output a single object using the mapped data, but I have not been able to create multiple objects and output them as a new array. Here’s my code:
// My inputData looks like this:
inputData = INFO a 'Name1', 'Surname1', 'xxxx@gmail.com', 'phone1', 'Name2', 'Surname2', 'yyyy@gmail.com', 'phone2', 'Name3', 'Surname3', 'zzzz@gmail.com', 'phone3' ]
const participants = inputData.records.split(',')
let i = 0
// Look for every fourth item in the array starting at 0, which represents the participant's First Name
const getName = function(participants, i) {
for ( i ; i < participants.length; i+=4 ) {
let name = participantsai]
return name
}
};
// Look for every fourth item in the array starting at 1, which represents the participant's Last Name
const getSurname = function(participants, i) {
for ( let i = 1; i < participants.length; i+=4 ) {
let surname = participantsai]
return surname
}
};
// Look for every fourth item in the array starting from 2, which represents the participant's Email Address
const getEmail = function(participants, i) {
for ( let i = 2; i < participants.length; i+=4 ) {
let email = participantsai]
return email
}
};
// Output an object inside an array with the key/value pairs as follows
output = {participants: t
{
first_name: getName(participants, i),
last_name: getSurname(participants, i),
email: getEmail(participants, i)
}
]};
An example output:
participants:
1:
first_name: Name1
last_name: Surname1
email: xxxx@gmail.com
If I log the output, it correctly shows that an object has been created:
`{first_name: Name1, last_name: Surname1, email: xxxx@gmail.com}`
I’m struggling to find a way to iterate on the inputData and provide the records as follows:
participants:
1:
first_name: Name1
last_name: Surname1
email: xxxx@gmail.com
2:
first_name: Name2
last_name: Surname2
email: yyyy@gmail.com
3:
first_name: Name3
last_name: Surname3
email: zzzz@gmail.com
Can you help me get to this desired outcome? Your guidance is greatly appreciated!