You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Pivot.js解析指定格式的JSON数组并遍历所有数据?

Hey there! Let me walk you through how to parse and iterate through your JSON job data using Pivot.js. It's pretty straightforward once you get the hang of it—here's a step-by-step breakdown with practical code examples:

Step 1: Set up your JSON data

First, let's make sure your data is stored in a usable variable. Here's how you can structure it:

const jobData = {
  "jobs": [
    {
      "id": "some_random_id_1",
      "email": "some@email.com",
      "email_type": "html",
      "status": "pending",
      "job_fields": { "TITLE": "job title here" },
      "stats": { "applied": 40, "rejected": 20 }
    },
    {
      "id": "some_random_id_2",
      "email": "some@email.com",
      "email_type": "html",
      "status": "pending",
      "job_fields": { "TITLE": "job title here" },
      "stats": { "applied": 30, "rejected": 20 }
    },
    // Add any additional job entries here
  ]
};
Step 2: Create a Pivot.js collection

Pivot.js works with collections of data, so we'll convert your jobs array into a Pivot collection. This gives you access to all of Pivot's handy methods:

// Initialize a Pivot collection from the jobs array
const jobCollection = pivot(jobData.jobs);
Step 3: Iterate through every job entry

Now you can use Pivot's forEach method to loop through each job and extract the data you need. This works just like the native array forEach, but with Pivot's extra utilities:

// Loop through each job in the collection
jobCollection.forEach(job => {
  // Access top-level properties directly
  console.log(`Job ID: ${job.id}`);
  console.log(`Contact Email: ${job.email}`);
  console.log(`Job Status: ${job.status}`);
  
  // Grab nested data from job_fields
  console.log(`Job Title: ${job.job_fields.TITLE}`);
  
  // Pull stats for candidates
  console.log(`Total Applied: ${job.stats.applied}`);
  console.log(`Total Rejected: ${job.stats.rejected}`);
  
  console.log('------------------------'); // Add a separator for readability
});
Step 4: Bonus: Filter or aggregate data (optional)

One of the perks of Pivot.js is how easy it makes filtering or aggregating your data. Here are a couple of quick examples:

// Filter to only get jobs with "pending" status
const pendingJobs = jobCollection.filter(job => job.status === 'pending');
console.log(`Total pending jobs: ${pendingJobs.length}`);

// Calculate the total number of applied candidates across all jobs
const totalAppliedCandidates = jobCollection.reduce((total, job) => total + job.stats.applied, 0);
console.log(`Total applied candidates overall: ${totalAppliedCandidates}`);

Just remember to make sure you've included the Pivot.js library in your project before running this code—either via a script tag in the browser or by importing it in a Node.js environment.

内容的提问来源于stack exchange,提问作者user263291

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:33:25