我是JavaScript的新手,在学习JavaScript时,它试图根据教育程度过滤员工,但我的过滤器返回的是空值。谁能帮我理解为什么会这样吗?
var employeeEdu = [{education: 'Masters'}];
var employees = [{id: 1, age: 35, name: 'James', dept: 'IT', education: 'Masters'},
{id: 2, age: 25, name: 'David', dept: 'Accounts', education: 'High School'},
{id: 3, age: 45,name: 'Tim', dept: 'HR', education: 'Graduate'},
{id: 4, age: 50,name: 'Vinod', dept: 'IT', education: 'PHD'}];
function chooseQualified(arrEmployee, empEducation) {
return arrEmployee.filter(function(emp) {
return emp.education === empEducation.education;
// return emp.education === 'Masters';
});
}
console.log(chooseQualified(employees, employeeEdu));
答案 0 :(得分:2)
这是因为employeeEdu
是一个数组,而employeeEdu.education
是未定义的。您需要做的是结帐employeeEdu[0].education
:
var employeeEdu = [{education: 'Masters'}];
var employees = [{"id":1,"age":35,"name":"James","dept":"IT","education":"Masters"},{"id":2,"age":25,"name":"David","dept":"Accounts","education":"High School"},{"id":3,"age":45,"name":"Tim","dept":"HR","education":"Graduate"},{"id":4,"age":50,"name":"Vinod","dept":"IT","education":"PHD"}];
function chooseQualified(arrEmployee, empEducation) {
return arrEmployee.filter(function(emp) {
return emp.education === empEducation[0].education;
// return emp.education === 'Masters';
});
}
console.log(chooseQualified(employees, employeeEdu));
另一种解决方案是删除包装数组:
employeeEdu = {education: 'Masters'};
答案 1 :(得分:0)
就像@Ori Drori说的那样,employeeEdu是一个数组,因此empEducation.education是未定义的,而empEducation [0] .education是“ Masters”。我建议employeeEdu是一个对象而不是数组,如下面的代码所示。 var employeeEdu = {education: 'Masters'};
var employeeEdu = {education: 'Masters'};
var employees = [{id: 1, age: 35, name: 'James', dept: 'IT', education: 'Masters'},
{id: 2, age: 25, name: 'David', dept: 'Accounts', education: 'High School'},
{id: 3, age: 45,name: 'Tim', dept: 'HR', education: 'Graduate'},
{id: 4, age: 50,name: 'Vinod', dept: 'IT', education: 'PHD'}];
function chooseQualified(arrEmployee, empEducation) {
return arrEmployee.filter(function(emp) {
return emp.education === empEducation.education;
// return emp.education === 'Masters';
});
}
console.log(chooseQualified(employees, employeeEdu));
答案 2 :(得分:0)
/* @Params: * array0 [Array of Objects]: Array to search through. * array1 [Array of Objects]: Array with the key/value to search for. * key [String]: Key of the object in array1. * index [Number](optional): Index of array1. default: 0. */ // Returns a new array of objects witch matched by key/value from the two given arrays. function findByKV(array0, array1, key, index = 0) {
// Get the value of the key to be searched for var value = array1[index][key];
// Filter the array to be searched obj is each Object in array1
array0.filter(function(obj) {
// Get each obj key of array0 and ...
return Object.keys(obj).some(function(key) {
// ...return true if at least one String value matches the value of the key in array1
return obj[key].toString().indexOf(value) != -1;
var target0 = [{education: 'Masters'}];
var target1 = [{dept: 'IT',education: ''}];
var employees = [
{id: 1,age: 35,name: 'James',dept: 'IT',education: 'Masters'},
{id: 2,age: 25,name: 'David',dept: 'Accounts',education: 'High School'},
{id: 3,age: 45,name: 'Tim',dept: 'HR',education: 'Graduate'},
{id: 4,age: 50,name: 'Vinod',dept: 'IT',education: 'PHD'},
{id: 5,age: 46,name: 'Matt',dept: 'IT',education: 'Masters'}
];
function findByKV(array0, array1, key, index = 0) {
var value = array1[index][key];
var array2 = array0.filter(function(obj) {
return Object.keys(obj).some(function(key) {
return obj[key].toString().indexOf(value) != -1;
});
});
return array2;
}
var result0 = findByKV(employees, target0, 'education');
var result1 = findByKV(employees, target1, 'dept');
console.log('Found targrt0: ' + JSON.stringify(result0, null, 2));
console.log('Found target1: ' + JSON.stringify(result1, null, 2));