Hey there, I would love to hear your thoughts on this.
Summary
I am seeking advice on how to extract multiple objects from an array using JavaScript and the Code by Zapier app. While I have successfully extracted data from the array's string using the .split()
method and output a single object with mapped data, I'm struggling to create multiple objects and compile them into a new array.
The Script
// Here is what my inputData looks like:
inputData = INFO [ 'Name1', 'Surname1', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="255d5d5d5d654248444c490b464a48">[email protected]</a>', 'phone1', 'Name2', 'Surname2', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d2abababab92b5bfb3bbbefcb1bdbf">[email protected]</a>', 'phone2', 'Name3', 'Surname3', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2953535353694e44484045074a4644">[email protected]</a>', 'phone3' ]
const participants = inputData.records.split(',')
let i = 0
// Find every fourth item in the array starting at index 0, which represents the participant's First Name
const getName = function(participants, i) {
for (i; i < participants.length; i += 4 ) {
let name = participants[i]
return name
}
};
// Find every fourth item in the array starting at index 1, which represents the participant's Last Name
const getSurname = function(participants, i) {
for ( let i = 1; i < participants.length; i += 4 ) {
let surname = participants[i]
return surname
}
};
// Find every fourth item in the array starting at index 2, which represents the participant's Email Address
const getEmail = function(participants, i) {
for ( let i = 2; i < participants.length; i += 4 ) {
let email = participants[i]
return email
}
};
// Output an object inside an array with the key/value pairs as shown below
output = {participants: [
{
first_name: getName(participants, i),
last_name: getSurname(participants, i),
email: getEmail(participants, i)
}
]};
Example Output:
participants:
1:
first_name: Name1
last_name: Surname1
email: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b2cacacacaf2d5dfd3dbde9cd1dddf">[email protected]</a>
Upon logging the output, it correctly displays that an object has been created:
{first_name: 'Name1', last_name: 'Surname1', email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="abd3d3d3d3ebccc6cac2c785c8c4c6">[email protected]</a>'}
My Challenge
I am facing difficulty in iterating through the inputData and presenting the records in the following format:
participants:
1:
first_name: Name1
last_name: Surname1
email: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="740c0c0c0c341319151d185a171b19">[email protected]</a>
2:
first_name: Name2
last_name: Surname2
email: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="433a3a3a3a03242e222a2f6d202c2e">[email protected]</a>
3:
first_name: Name3
last_name: Surname3
email: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c4bebebebe84a3a9a5ada8eaa7aba9">[email protected]</a>
When I use console.log()
for each function's output, I get each record from the for
loops. However, it does not output a record for each iteration. Why might this be happening?