Javascript 阵列辅助筛选器返回空值的原因

Javascript 阵列辅助筛选器返回空值的原因,javascript,ecmascript-6,Javascript,Ecmascript 6,我是JavaScript新手,在学习JavaScript时,我尝试根据教育程度过滤员工,但我的过滤器返回空值。有人能帮我理解为什么会这样吗 var employeeEdu=[{education:'Masters'}]; var employees=[{id:1,年龄:35,姓名:'James',部门:'IT',教育:'Masters'}, {id:2,年龄:25,姓名:'David',部门:'Accounts',教育:'High School'}, {id:3,年龄:45,姓名:'Tim',

我是JavaScript新手,在学习JavaScript时,我尝试根据教育程度过滤员工,但我的过滤器返回空值。有人能帮我理解为什么会这样吗

var employeeEdu=[{education:'Masters'}];
var employees=[{id:1,年龄:35,姓名:'James',部门:'IT',教育:'Masters'},
{id:2,年龄:25,姓名:'David',部门:'Accounts',教育:'High School'},
{id:3,年龄:45,姓名:'Tim',部门:'HR',教育:'Graduate'},
{id:4,年龄:50,姓名:'Vinod',部门:'IT',教育:'PHD'}];
功能选择合格(雇员、学历){
返回Aremployee.filter(函数(emp){
返回emp.education==empeucation.education;
//返回emp.education==‘Masters’;
});
}

console.log(选择已认证(员工、员工教育))
这是因为
employeedu
是一个数组,
employeedu.education
是未定义的。您需要做的是签出员工教育[0]。教育:

var employeeEdu=[{education:'Masters'}];
var员工=[{“id”:1,“年龄”:35,“姓名”:“詹姆斯”,“部门”:“IT”,“教育”:“硕士”},{“id”:2,“年龄”:25,“姓名”:“大卫”,“部门”:“会计”,“教育”:“高中”},{“id”:3,“年龄”:45,“姓名”:“蒂姆”,“部门”:“人力资源”,“教育”:“毕业生”},{“id”:4,“年龄”:50,“姓名”:“维诺德”,“部门”:“IT”,“教育”:“博士”};
功能选择合格(雇员、学历){
返回Aremployee.filter(函数(emp){
返回emp.education==empeucation[0]。education;
//返回emp.education==‘Masters’;
});
}

console.log(选择已认证(员工、员工教育))就像@Ori Drori说的,employeeEdu是一个数组,所以empEducation.education是未定义的,但是empEducation[0]。education是“Masters”。我建议Employeedu是一个对象而不是数组,如下面的代码所示<代码>var employeeEdu={education:'Masters'}

var employeeEdu={education:'Masters'};
var employees=[{id:1,年龄:35,姓名:'James',部门:'IT',教育:'Masters'},
{id:2,年龄:25,姓名:'David',部门:'Accounts',教育:'High School'},
{id:3,年龄:45,姓名:'Tim',部门:'HR',教育:'Graduate'},
{id:4,年龄:50,姓名:'Vinod',部门:'IT',教育:'PHD'}];
功能选择合格(雇员、学历){
返回Aremployee.filter(函数(emp){
返回emp.education==empeucation.education;
//返回emp.education==‘Masters’;
});
}
console.log(选择已认证(员工、员工教育))







var target0=[{education:'Masters'}];
var target1=[{dept:'IT',education:'}];
var雇员=[
{id:1,年龄:35,姓名:'James',部门:'IT',教育:'Masters'},
{id:2,年龄:25,姓名:'David',部门:'Accounts',教育:'High School'},
{id:3,年龄:45,姓名:'Tim',部门:'HR',教育:'Graduate'},
{id:4,年龄:50,姓名:'Vinod',系:'IT',教育:'PHD'},
{id:5,年龄:46,姓名:'Matt',部门:'IT',教育:'Masters'}
];
函数findByKV(array0,array1,key,index=0){
var值=数组1[索引][键];
var array2=array0.过滤器(功能(obj){
返回Object.key(obj).some(函数(key){
返回obj[key].toString().indexOf(value)!=-1;
});
});
返回阵列2;
}
var result0=findByKV(员工,目标0,“教育”);
var result1=findByKV(员工,目标1,“部门”);
log('Found target0:'+JSON.stringify(result0,null,2));

log('Found target1:'+JSON.stringify(result1,null,2))
它似乎返回一个空数组,而不是
null
。要自己查找问题,可以在代码中添加更多
console.log()
语句。有关调试自己的代码的更多提示,请阅读这篇文章。它返回的是未定义的,不是null。谢谢分享链接。我将对此进行检查。或者,将
employeedu
指定为对象,或者直接将教育字符串传递给函数。感谢您帮助我理解此问题。当我尝试基于您的解决方案时,它起了作用。:)或者,employeedu可以只是一个字符串。
/* @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;