最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript : filter array of objects using another array as filter - Stack Overflow

programmeradmin2浏览0评论

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 badges
Add a ment  | 

3 Answers 3

Reset to default 5

You 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!

发布评论

评论列表(0)

  1. 暂无评论