首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Zapier -从对象创建数组

Zapier -从对象创建数组
EN

Stack Overflow用户
提问于 2021-01-29 23:11:55
回答 1查看 87关注 0票数 0

​您好,感谢您对此的见解。

概述

我正在寻找关于如何使用JavaScript和Zapier应用程序的代码从数组中输出多个对象的建议。到目前为止,我已经能够通过使用.split()方法从数组的字符串中提取数据,并使用映射的数据输出一个独立的对象,但是我还不能创建多个对象并将它们输出为一个新的数组。

代码

代码语言:javascript
运行
复制
// 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)
}
]};

输出示例:

代码语言:javascript
运行
复制
participants:
   1:
     first_name: Name1
     last_name: Surname1
     email: xxxx@gmail.com

如果我记录输出,它会正确地显示已经创建了一个对象:

{first_name: 'Name1', last_name: 'Surname1', email: 'xxxx@gmail.com'}

我的奋斗

我正在努力寻找一种在inputData上迭代的方法,并提供如下记录:

代码语言:javascript
运行
复制
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循环中得到每条记录。那么为什么它不为每个人输出一条记录呢?

EN

回答 1

Stack Overflow用户

发布于 2021-01-31 01:42:49

所以我设法得到了想要的输出,如下所示:

代码语言:javascript
运行
复制
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}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65957039

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档