您好,感谢您对此的见解。
概述
我正在寻找关于如何使用JavaScript和Zapier应用程序的代码从数组中输出多个对象的建议。到目前为止,我已经能够通过使用.split()
方法从数组的字符串中提取数据,并使用映射的数据输出一个独立的对象,但是我还不能创建多个对象并将它们输出为一个新的数组。
代码
// 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)
}
]};
输出示例:
participants:
1:
first_name: Name1
last_name: Surname1
email: xxxx@gmail.com
如果我记录输出,它会正确地显示已经创建了一个对象:
{first_name: 'Name1', last_name: 'Surname1', email: 'xxxx@gmail.com'}
我的奋斗
我正在努力寻找一种在inputData上迭代的方法,并提供如下记录:
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
当我console.log()
每个函数的输出时,我会从for
循环中得到每条记录。那么为什么它不为每个人输出一条记录呢?
发布于 2021-01-31 01:42:49
所以我设法得到了想要的输出,如下所示:
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}
https://stackoverflow.com/questions/65957039
复制相似问题