admin管理员组文章数量:1379578
Suppose each employee is a object having
emp1.age=30
emp1.name="Hang"
emp2.age=40
emp2.name="Dang"
emp3.age=50
emp3.name="Bang"
storing it in Array.
var orginialArray=[emp1,emp2,emp3];
I want to filter or splice the array with the other array of strings.
Somewhere I'm preparing an array of names to pare with originalArray
and then remove the unmatching employee from originalArray
var removeElements=["Hang","Dang"]
I tried filter
but it ends up sending a string in the callback and I could not make it work
function filterItems(query) {
return orginialArray.filter((el) =>
el.toLowerCase().indexOf(query.toLowerCase()) > -1
)
}
console.log(filterItems(removeElements)); //i know this is wrong
Objective is to filter the original array and only have the employee whose name is not in remove element array.
I also tried splice but also no luck.
Suppose each employee is a object having
emp1.age=30
emp1.name="Hang"
emp2.age=40
emp2.name="Dang"
emp3.age=50
emp3.name="Bang"
storing it in Array.
var orginialArray=[emp1,emp2,emp3];
I want to filter or splice the array with the other array of strings.
Somewhere I'm preparing an array of names to pare with originalArray
and then remove the unmatching employee from originalArray
var removeElements=["Hang","Dang"]
I tried filter
but it ends up sending a string in the callback and I could not make it work
function filterItems(query) {
return orginialArray.filter((el) =>
el.toLowerCase().indexOf(query.toLowerCase()) > -1
)
}
console.log(filterItems(removeElements)); //i know this is wrong
Objective is to filter the original array and only have the employee whose name is not in remove element array.
I also tried splice but also no luck.
Share Improve this question edited May 19, 2017 at 16:27 Mohit Bhardwaj 10.1k7 gold badges40 silver badges65 bronze badges asked May 19, 2017 at 15:08 NeverGiveUp161NeverGiveUp161 87013 silver badges36 bronze badges3 Answers
Reset to default 5You can use Array.filter() method (which you are already using) and return true only if current employees name does not exist in removeNames array(which you may check by using Array.indexOf() ):
var employees = [
{"name": "abc", "age":"34"},
{"name": "pqr", "age":"34"},
{"name": "xyz", "age":"34"},
{"name": "xxx", "age":"34"},
];
var removeNames = ["xxx"];
// if required, make all removeNames elements lowerCase first
// removeNames = removeNames.map(function(name){
// return name.toLowerCase();
// });//map()
var filteredEmployees = employees.filter(function(emp){
return removeNames.indexOf(emp.name.toLowerCase()) === -1;
});//filter
console.log( filteredEmployees );
You're close. Since query
is an array of names, you need to check if the current employee's name is in that array - if so, exclude that employee from the new list, like so:
var emp1 = {
age: 30,
name: "Hang"
};
var emp2 = {
age: 40,
name: "Dang"
};
var emp3 = {
age: 50,
name: "Bang"
};
var originalArray = [emp1, emp2, emp3];
var removeElements = ["hang", "bang"];
function filterItems(query) {
return originalArray.filter((employee) =>
query.indexOf(employee.name.toLowerCase()) === -1
)
}
console.log(filterItems(removeElements));
this is a fully ES6 array method solution. You could do something like this:
filterItems(query){
let newArray = []
query.map( (qElem) => newArray.push(qElem.toLowerCase()) )
return originalArray.filter((el) =>
newArray.includes(el.toLowerCase())
)
}
I'm generating a new array with the query names but lowercase. Then return a new array with all the elements that do not satisfy the condition of being included in the newArray. Hope It helps!
本文标签: javascriptfilter array of objects using another array as filterStack Overflow
版权声明:本文标题:javascript : filter array of objects using another array as filter - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744499937a2609262.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论