使用打字稿中的动态值进行过滤

时间:2020-04-13 18:07:01

标签: javascript arrays angular typescript filter

我的要求是将所有Portfolio Lead过滤为English Competency,并将值过滤为No

 var data = [{
    "Employee Number": 138,
    "English Competency": "No",
    "Portfolio Lead": "x",
    "Maths Competency": "No"
  },
  {
    "Employee Number": 1385,
    "English Competency": "yes",
    "Portfolio Lead": "x",
    "Maths Competency": "yes"
  },
  {
    "Employee Number": 1318,
    "English Competency": "yes",
    "Portfolio Lead": "y",
    "Maths Competency": "No"
  },
  {
    "Employee Number": 1388,
    "English Competency": "No",
    "Portfolio Lead": "y",
    "Maths Competency": "yes"
  },
  {
    "Employee Number": 1388,
    "English Competency": "No",
    "Portfolio Lead": "z",
    "Maths Competency": "no"
  }
];

******************更新**************************** ****

下面的代码可以正常工作,但是在employeeData中,它只是显示数字而不是实际数据。请帮忙

var leadsList = ['x','y','z']

function getcompetencyData(name) {

  let filteredCompetency = data.filter(item => item[name] == "No");
  console.log(filteredCompetency);

  let leadData = [];
  let employeeData = [];
  leadsList.forEach(lead => {
    filteredCompetency.forEach(competency => {
      if (lead === competency["Portfolio Lead"]) {
        leadData.push({ "Portfolio Lead": lead, "employeeData": employeeData.push(competency) })
      }
    })
  });
  console.log(leadData);
}

getcompetencyData('English Competency');

请告诉我我在做什么错。下面是stackblitz链接

https://stackblitz.com/edit/typescript-n5j5y6

3 个答案:

答案 0 :(得分:0)

尝试这样。

var data = [{
    "Employee Number": 138,
    "English Competency": "No",
    "Portfolio Lead": "x",
    "Maths Competency": "No"
  },
  {
    "Employee Number": 1385,
    "English Competency": "yes",
    "Portfolio Lead": "x",
    "Maths Competency": "yes"
  },
  {
    "Employee Number": 1318,
    "English Competency": "yes",
    "Portfolio Lead": "y",
    "Maths Competency": "No"
  },
  {
    "Employee Number": 1388,
    "English Competency": "No",
    "Portfolio Lead": "y",
    "Maths Competency": "yes"
  },
  {
    "Employee Number": 1388,
    "English Competency": "No",
    "Portfolio Lead": "z",
    "Maths Competency": "no"
  }
];

let x = [];
let y = [];
let z = [];

function filterData(key, val) {
    data.map(lead => {
        if(lead[key] === val) {
         if (lead['Portfolio Lead'] === 'x') return x.push(lead)
         if (lead['Portfolio Lead'] === 'y') return y.push(lead)
         if (lead['Portfolio Lead'] === 'z') return z.push(lead)  
        }
        return
    })
}

filterData('Maths Competency', 'No');
console.log(x, y, z);

filterData('English Competency', 'No');
console.log(x, y, z);

或者您也可以在函数中将Portfolio Lead值作为参数传递。 让我知道您是否还有疑问。

答案 1 :(得分:0)

最好的选择是创建两个对象,并通过迭代数据来填充它们。

上面的答案可能会给出错误的结果,因为它不会清除列表x,y和z。相反,请尝试以下操作:

const data = [
  {
    "Employee Number": 138,
    "English Competency": "No",
    "Portfolio Lead": "x",
    "Maths Competency": "No"
  },
  {
    "Employee Number": 1385,
    "English Competency": "yes",
    "Portfolio Lead": "x",
    "Maths Competency": "yes"
  },
  {
    "Employee Number": 1318,
    "English Competency": "yes",
    "Portfolio Lead": "y",
    "Maths Competency": "No"
  },
  {
    "Employee Number": 1388,
    "English Competency": "No",
    "Portfolio Lead": "y",
    "Maths Competency": "yes"
  },
  {
    "Employee Number": 1388,
    "English Competency": "No",
    "Portfolio Lead": "z",
    "Maths Competency": "no"
  }
];

// Create empty objects to count results in
const English = {}
const Maths = {}

// Iterate over each data entry
for (let entry of data) {
  // Load properties into convenient variable names
  id = entry["Employee Number"]
  english = entry["English Competency"]
  lead = entry["Portfolio Lead"]
  math = entry["Maths Competency"]

  // Create empty list for English and Maths objects if key not present
  if (!English[lead]) {
    English[lead] = []
  }
  if(!Maths[lead]) {
    Math[lead] = []
  }

  // Add employee to lists based on competency and lead
  if (english == "yes") {
    English[lead].push(id)
  }
  if (math == "yes") {
    Math[lead].push(id)
  }
}

// Display results for English
console.log("Project Lead: | English Competency:")
for (let [lead, emps] of Object.entries(English)) {
  console.log(lead, "       ",emps.length)
}
// Display results for Maths
console.log("Project Lead: | Maths Competency:")
for (let [lead, emps] of Object.entries(Math)) {
  console.log(lead, "       ",emps.length)
}

答案 2 :(得分:0)

使用过滤器并应用地图进行转换的简单解决方案:

ph1