0

I've got this object:

this.contacts = [
            {
                name: "",
                el: ["one", "two"]
            },
            {
                name: "",
                el: ["three", "four"]
            }]

How to filter the above so for example only the first object is returned when I search for "two"?

My try so far:

filterItems(searchTerm) {

    return this.contacts.filter((item) => {
        return item.el.filter((user) => {
            return user.toLowerCase().indexOf(searchTerm.toLowerCase()) > -1;
        })
    });

}

It doesn't return anything of course, why?

1
  • If you have another object with 'two' in it, should it return that object as well or should it stop after first successful match? Commented Oct 17, 2017 at 19:14

3 Answers 3

2

You could use Array#includes and take it as filter condition.

function filterItems(user) {
    return this.contacts.filter(item => item.el
        .map(s => s.toLowerCase())
        .includes(user.toLowerCase()))
}

var contacts = [{ name: "", el: ["one", "two"] }, { name: "", el: ["thrEE", "four"] }];

console.log(filterItems('THREE'));
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Comments

1

Inside the first .filter you should use .some

var contacts = [{
    name: "",
    el: ["one", "two"]
  },
  {
    name: "",
    el: ["three", "four"]
  }
]

filterItems = function(searchTerm) {
  return contacts.filter((item) => {
    return item.el.some((user) => {
      return user.toLowerCase().indexOf(searchTerm.toLowerCase()) > -1;
    })
  });
}

console.log(filterItems("two"))
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

0

If you wish to achieve a similar effect but by using loops you could use the following function:

function filterItems(search) {
    for(let i = 0; i < this.contacts.length; i++) {
        let obj = this.contacts[i];
        for(let j = 0; j < obj.el.length; j++) {
            if(obj.el[j] === search) return i;
        }
    }
}

console.log(filterItems("two")); // logs 0 (returns 0) 
console.log(filterItems("four")); // logs 1 (returns 1)

Essentially the first loop is used to loop through your contacts array. And then the second is used to loop through the array in the el property. Then upon each iteration of the el array, we can check if the array element we are currently on equals the passed through search value, thus returning true if it matches

Working example: https://jsfiddle.net/knLbL9b5/ (Check console for outputs)

Comments

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.