[英]extract values from the array according to the Key and values
I have a requirement to extract values from the array according to the occupation into different excel sheets.我需要根据职业从数组中提取值到不同的 excel 表中。
{ age: 22, name: "Jack McDonald", occupation: "Developer" },
{ age: 22, name: "Bridget Williams", occupation: "Developer" },
{ age: 22, name: "Nathan Thomas", occupation: "Developer" },
{ age: 23, name: "Olivia Beck", occupation: "Tester" },
{ age: 24, name: "Derek wilson", occupation: "Tester" }
{ age: 24, name: "Derek wilson", occupation: "BA" }
{ age: 24, name: "Derek wilson", occupation: "IT" }
{ age: 24, name: "Derek wilson", occupation: "IT" }
I want an excel sheet with the name Developer.csv and should only contain Developer items likewise, I need another excel sheet with the name Tester.csv and should only contain tester items and so on I want an excel sheet with the name Developer.csv and should only contain Developer items likewise, I need another excel sheet with the name Tester.csv and should only contain tester items and so on
I am stuck here.我被困在这里。 please help请帮忙
Suppose this is an array you can use Array.filter to match all the needed occupations.假设这是一个数组,您可以使用Array.filter来匹配所有需要的职业。 Here is a sample code based on the info you provided这是基于您提供的信息的示例代码
const a = [{ age: 22, name: "Jack McDonald", occupation: "Developer" }, { age: 22, name: "Bridget Williams", occupation: "Developer" }, { age: 22, name: "Nathan Thomas", occupation: "Developer" }, { age: 23, name: "Olivia Beck", occupation: "Tester" }, { age: 24, name: "Derek wilson", occupation: "Tester" }, { age: 24, name: "Derek wilson", occupation: "BA" }, { age: 24, name: "Derek wilson", occupation: "IT" }, { age: 24, name: "Derek wilson", occupation: "IT" }] const developers = a.filter(x=>x.occupation === 'Developer') const testers = a.filter(x=>x.occupation === 'Tester') console.log(developers) console.log(testers)
As I see your problem has 2 parts:我看到你的问题有两个部分:
occupation
are without duplicates首先你需要知道有多少occupation
没有重复const noDupOccup = [...new Set(arr.map(o=>o.occupation))]
occupation
然后你需要为每个occupation
提供一个过滤数组for (occ on noDupOccup){
const cvs = arr.filter(o=>o.occupation === occ)
// save your 'occ'.cvs here with cvs data
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.