1

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 compare 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.

3 Answers 3

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 );

Sign up to request clarification or add additional context in comments.

8 Comments

inside the filter if i want to store the matching name in filtered employees
@NeverGiveUp161 sorry I did not get that. Do you mean that you want to store the removed employees in a separate array? Also, I think you should create a separate question for a separate problem as it may help other people also.
its just a minor change,that is the reason.So filteredEmployees now have everything except removeNames content.But i want filteredEmployees should have only matching element in removeNames array.I think tweaking return removeNames.indexOf(emp.name.toLowerCase()) === -1; would help?
Sorry I still did not get the exact requirement. You mean you want the logic reversed? That is, now only those elements should be kept which ARE IN removeNames array and others should be filtered out?
okay :). You just need to change the equality operator to not equal operator i.e. use return removeNames.indexOf(emp.name.toLowerCase()) !== -1; instead.
|
3

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));

6 Comments

There's nothing wrong with this answer, and I personally didn't downvote it. My guess is you might be getting downvotes due to the fact that it isn't following a functional paradigm.
@doutriforce There is nothing wrong with the answer and I didn't downvote and I don't disagree with you at all that it could be helpful but as I said, I assume who ever downvoted did not find the answer useful and they don't have to explain why as I'm sure they would have left a comment already if they felt it was worth it to them.
@zfrisch I agree, my attempt was to keep the code as close to the OP's original code as possible.
@zfrisch I wasn't the one asking about the downvotes, however. lol someone came by and downvoted every answer and then left - it happens occasionally for no reason. I know my solution is beneficial to the OP, so I'm not too worried about it.
@NeverGiveUp161 If === -1 gives you the unmatched, and "not unmatched" gives you matched, then not === -1 (AKA !== -1) will give you the matched. You can also say > -1 to save a couple keystrokes. Or even fancier, ~query.indexOf(...) The tilde operator does a bitwise NOT, and only returns false if the value is equal to -1, and returns true for everything else. Which in the case of indexOf() is actually perfect (it is just more ambiguous as to what the code is doing)
|
-1

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!

1 Comment

map should not be used for iterating, and in fact, you can use it for its intended purpose (creating a new array with all of the elements of the original array, transformed) easily: let newArray = query.map((qElem) => qElem.toLowerCase());.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.