Best answer

[JavaScript] Output Array of Objects from Line Items

  • 29 January 2021
  • 2 replies
  • 1420 views

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 [ '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 = participants[i]

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 = participants[i]

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 = participants[i]

return email
}
};

// Output an object inside an array with the key/value pairs as follows
output = {participants: [
{
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!

 

 

icon

Best answer by ZeroGDev 30 January 2021, 18:40

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

2 replies

I’ve managed to make it work like this:

const participants = inputData.records.split(',');
const size = participants.length;

// This sets up the iterator according to the lenght of the 'participants' array
class Sequence {
constructor( start = 0, end = Infinity, interval = 1 ) {
this.start = start;
this.end = end;
this.interval = interval;
}
[Symbol.iterator]() {
let counter = 0;
let nextIndex = this.start;
return {
next: () => {
if ( nextIndex <= this.end ) {
let result = { value: nextIndex, done: false }
nextIndex += this.interval;
counter++;
return result;
}
return { value: counter, done: true };
}
}
}
};

let range = new Sequence(0, size - 4, 4);

// Creates an array from the generated Sequence which sets up the iteration count
const startIndex = Array.from(range)

// Create a 'participant' object using the formula
const participant = function(arr, num) {
let name = participants[num]
let surname = participants[num+1]
let email = participants[num+2]
return {name: name, surname: surname, email: email}
};

// creates an empty array and then pushes the data with every cycle
let participantRecords = []
const participantsList = startIndex.forEach(function(num){
participantRecords.push(participant(participants, num))
});


output = {participantRecords}

 

Userlevel 7
Badge +11

Hey @ZeroGDev!

Glad you were able to get that working! And thanks for sharing that here with the Community — much appreciated :)