6

I have this code, which filters my data. I'm asking me, if there is a way not to filter each field (id, mandant, zonenlogik...) explicitly. Maybe there is a more smooth way to set the filter on all fields without calling them explicitly?

let filteredList = this.state.freights.filter((freight) => {

    if (freight.id.toLowerCase().indexOf(this.state.search.toLowerCase()) !== -1) {
        return freight;
    }
    if (freight.mandant.toLowerCase().indexOf(this.state.search.toLowerCase()) !== -1) {
        return freight;
    }
    if (freight.zonenlogik.toLowerCase().indexOf(this.state.search.toLowerCase()) !== -1) {
        return freight;
    }
    if (freight.frachtart_nr.toLowerCase().indexOf(this.state.search.toLowerCase()) !== -1) {
        return freight;
    }
    if (freight.transportart_nr.toLowerCase().indexOf(this.state.search.toLowerCase()) !== -1) {
        return freight;
    }
    if (freight.spedit_nr.toLowerCase().indexOf(this.state.search.toLowerCase()) !== -1) {
        return freight;
    }
    if (freight.spedit2_nr.toLowerCase().indexOf(this.state.search.toLowerCase()) !== -1) {
        return freight;
    }
    if (freight.lager_nr.toLowerCase().indexOf(this.state.search.toLowerCase()) !== -1) {
        return freight;
    }
});
1
  • To simplify the codes, first you can reuse the variables like this.state.search.toLowerCase(). Also, you can group the similar logic with a function. Commented Jun 23, 2017 at 5:51

3 Answers 3

6

You can fetch the object values using Object.values() and then loop over those to check whether a substring is present in the string and then return the filtered object

let filteredList = this.state.freights.filter((freight) => {
    let search = this.state.search.toLowerCase();
    var values = Object.values(freight);
    var flag = false
    values.forEach((val) => {
      if(val.toLoweCase().indexOf(search) > -1) {
           flag = true;
           return;
       }
     }
     if(flag) return freight
});
Sign up to request clarification or add additional context in comments.

1 Comment

Just a small update you can't toLowerCase() on non-string values, so you should test to make sure you have a string in the if block for the indexOf just to be safe and you have "toLoweCase", it should be "toLowerCase".
1

I'm assuming that you want another code that achieves the same result as the code you posted. Here is mine:

const searchTerm = this.state.search.toLowerCase();

let filteredList = this.state.freights.filter((freight) => {
    // get all keys of freight
    const keys = Object.keys(freight).map(k => k.toLowerCase());
    for (let k of keys) {
      // if key (e.g id) matches the search term, return freight
      if (k.indexOf(searchTerm) !== 1) {
        return true; // we want this freight object
      } 
    }
    return false;
});

Comments

1

Here is the es5 one-liner (sort of):

let filteredList = this.state.freights.filter(freight =>
  Object.keys(freight).some(
    key =>
      freight[key]
        .toLowerCase()
        .indexOf(this.state.search.toLowerCase()) !== -1,
  ),
);

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.