[英]How to filter multiple Arrays with Javascript
我正在尝试过滤多个 arrays 技能(它们来自 json 文件)(下面的示例)并打印出候选人姓名+所有技能
┌─────────┬───────────────────────┐
│ (index) │ Values │
├─────────┼───────────────────────┤
│ 0 │ 'angular' │
│ 1 │ 'css' │
│ 2 │ 'bootstrap' │
│ 3 │ 'react.js' │
│ 4 │ 'rxjs' │
│ 5 │ 'git' │
│ 6 │ 'java' │
│ 7 │ 'graphql' │
│ 8 │ 'node.js' │
│ 9 │ 'redux' │
│ 10 │ 'json' │
│ 11 │ 'html' │
│ 12 │ 'sql' │
│ 13 │ 'linux' │
│ 14 │ 'gatsby.js' │
│ 15 │ 'mongodb' │
│ 16 │ 'javascript' │
│ 17 │ 'php' │
│ 18 │ 'ionic' │
│ 19 │ 'amazon web services' │
└─────────┴───────────────────────┘
但不是所有技能,我只是获得单个值(1 个技能/1 行),并且 output 在控制台中看起来像这样:
这家伙知道 javascript
这家伙知道nodejs
这家伙知道反应
这家伙知道 javascript
但目标是在 1 行中拥有所有技能,如下所示:
这家伙知道 javascript nodejs 反应
输入。json
{
"stackIncluded": [
"nodejs",
"javascript"
]
}
这是我编写的 function 的代码index.js
'use strict';
const curationInput = require('./curation-input.json');
const fileToCurate = require('./trlele.json');
async function curateStack() {
const stackIncluded = curationInput.stackIncluded;
fileToCurate.forEach(candidate => {
candidate.folder.forEach(folder => {
// get Arrays with all objects that including candidates skills
const allSkills = folder.skills;
// map Arrays with objects to Arrays with skills only
const values = allSkills.map(item => item.value);
// lowercase the values of skills
const lowercaseSkills = values.map(skill => skill.toLowerCase());
//console.table(lowercaseSkills)
lowercaseSkills.forEach(skillsArray => {
if (stackIncluded.includes(skillsArray)) {
console.log('this guys knows', skillsArray);
}
});
});
});
}
您需要更改脚本的最后一部分
let str = 'this guys knows '
lowercaseSkills.forEach(skillsArray => {
if (stackIncluded.includes(skillsArray)) {
str += skillsArray + ' ';
}
});
console.log(str);
另一种方式
const skills = lowercaseSkills.filter(item => stackIncluded.includes(item));
console.log('this guys knows', skills.join(', ')]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.