JavaScript Filtering through arrays and strings at the same time? filtering through array javascript,filtering in arrays,filtering in arraylist,

Here I am filtering through items:

let filter = this.props.data.filter(
  (item) => {
    return (
      item.fname.toLowerCase().indexOf(this.state.search.toLowerCase()) !== -1 ||
      item.lname.toLowerCase().indexOf(this.state.search.toLowerCase()) !== -1
    );
  }
);

So I am updating my table, whenever the user types something into the input field. However, there is not only fname and lname, there is also an array field called [children] which contains the names of the children the person has:

children : ["Bob", "Andrew", "Mary-Jane"]

I want to also filter if any of those names is typed in. How would I do this? I am a javascript / React beginner, so I would appreciate any help!

Answer:1

You can use some to check if any element in the array has your search term as a substring:

const search = this.state.search.toLowerCase();
let filter = this.props.data.filter(
  (item) => {
    return (
      item.fname.toLowerCase().includes(search) ||
      item.lname.toLowerCase().includes(search) ||
      item.children.some(child => 
        child.toLowerCase().includes(search)
      )
    );
  }
);
Answer:2

I have a text similar to the text below. It contains a 4 digits number that follows either digit- or whitespace and is followed by either ., ?, -digit or whitespace. I need to match all of the digits ...

I have a text similar to the text below. It contains a 4 digits number that follows either digit- or whitespace and is followed by either ., ?, -digit or whitespace. I need to match all of the digits ...

  1. regex find exact string
  2. regex find exact word
  3. regex find exact word in string
  4. regex find exact number of digits
  5. regex find exact match
  6. regex find exact match python

node.js/express code var express = require('express'), app = express(), port = process.env.PORT || 3000, path = require('path'); app.use('/public', function (req, res) { res.sendfile(...

node.js/express code var express = require('express'), app = express(), port = process.env.PORT || 3000, path = require('path'); app.use('/public', function (req, res) { res.sendfile(...

I am just confused about how to match two statement like if else of JavaScript in React Native for e.g., if(array.length != null && array.length >= 2){ alert("Array Is Greater Than 2")...

I am just confused about how to match two statement like if else of JavaScript in React Native for e.g., if(array.length != null && array.length >= 2){ alert("Array Is Greater Than 2")...

  1. write condition in excel
  2. write condition in sql
  3. write condition in css
  4. write condition in python
  5. write condition in html
  6. write condition in ngif
  7. write condition in r
  8. write condition in java
  9. write condition in tableau
  10. write condition in c#
  11. write condition in matlab
  12. write condition in angularjs
  13. write condition in oracle

I'm trying to have a page that switches out which component is rendered based off of a pair of navigation buttons. The page is set up like this ReactDOM.render(<Logo/>, document.getElementById(...

I'm trying to have a page that switches out which component is rendered based off of a pair of navigation buttons. The page is set up like this ReactDOM.render(<Logo/>, document.getElementById(...

  1. programmatically change route react
  2. programmatically change route react router
  3. react programmatically change style
  4. react programmatically change props