简体   繁体   English

如何使用 Javascript 过滤多个 Arrays

[英]How to filter multiple Arrays with Javascript

I am trying to filter multiple arrays of Skills (they are coming from json file) (example below) and print out the candidate name + all skills我正在尝试过滤多个 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' │
└─────────┴───────────────────────┘

but instead of all skills I'm just getting single values (1 skill/1 line) and the output looks in console looks like this:但不是所有技能,我只是获得单个值(1 个技能/1 行),并且 output 在控制台中看起来像这样:

this guys knows javascript这家伙知道 javascript
this guys knows nodejs这家伙知道nodejs
this guys knows react这家伙知道反应
this guys knows javascript这家伙知道 javascript

But the goal is to have all skills in 1 line like this:但目标是在 1 行中拥有所有技能,如下所示:

this guys knows javascript nodejs react这家伙知道 javascript nodejs 反应

input.json输入。json

{
    "stackIncluded": [
    "nodejs",
    "javascript"
    ]
}

Here is a code of the function that I've written, index.js这是我编写的 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);
        }
      });

    });
  });
}

You need to change the last part of your script您需要更改脚本的最后一部分

let str = 'this guys knows '
lowercaseSkills.forEach(skillsArray => {
    if (stackIncluded.includes(skillsArray)) {
        str += skillsArray + ' ';
    }
});
console.log(str);

another way另一种方式

const skills = lowercaseSkills.filter(item => stackIncluded.includes(item));
console.log('this guys knows', skills.join(', ')]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM