Filter on two arrays same time?

I have two arrays:

const array1 = [{
        "id": "4521",
        "name": "Tiruchirapalli",
        "stateId": "101"
      },
      {
        "id": "1850",
        "name": "Tenkasi",
        "stateId": "101"
      },
      {
        "id": "202",
        "name": "Thanjavur",
        "stateId": "101"
      },
      {
        "id": "505",
        "name": "Ernakulam",
        "stateId": "102"
      },
    ];

And now array2

 const array2 = [{
        "id": 1850,
        "cityName": "Tenkasi",
        "aliasNames": [
          "Thenkasi"
        ]
      },
      {
        "id": 4521,
        "cityName": "Tiruchirapalli",
        "aliasNames": [
          "Trichy"
        ]
      },
      {
        "id": 202,
        "cityName": "Thanjavur",
        "aliasNames": [
          "Tanjore"
        ]
      },
      {
        "id": 505,
        "cityName": "Ernakulam",
        "aliasNames": [
            "Kochi",
            "Cochin"
        ]
    },
    ];

what i need to do is, how to filter both the arrays at same time ( or filter first one and then second which ever one is performance effective ).

For instance, when user types "Kochi", first it should check on array1 to find if its has name="Kochi", if it has then we can set the state with that and if it doesnt have we need to find it on array2 and the update the state !

Which is fast and effective way to handle this – ( array1 has 2500 records and array2 has 990 records ) so performance / speed is also a concern

My attempt:

searchFilterFunction = text => {   
        this.setState({ typedText: text }); 
 
        const newData = array1.filter(item => {      
          const itemData = `${item.name.toUpperCase()}`;
           const textData = text.toUpperCase();
           return itemData.indexOf(textData) > -1;    
        });
        
        this.setState({ data: newData});  
      };

How to implement the second filter in optimized way ?

58 thoughts on “Filter on two arrays same time?”

  1. Hey just wanted to give you a quick heads up. The text in your
    post seem to be running off the screen in Firefox. I’m not
    sure if this is a format issue or something to do with internet browser compatibility but I figured I’d
    post to let you know. The design look great though!
    Hope you get the issue resolved soon. Kudos

    Reply
  2. When I originally left a comment I appear to have clicked on the -Notify me when new comments are
    added- checkbox and now whenever a comment is added I recieve 4 emails with the same comment.
    Is there a means you are able to remove me from that service?
    Thanks a lot!

    Reply
  3. Hey just wanted to give you a quick heads up.
    The words in your content seem to be running
    off the screen in Opera. I’m not sure if this
    is a format issue or something to do with internet browser compatibility but I
    figured I’d post to let you know. The style and design look great
    though! Hope you get the issue fixed soon. Many thanks

    Reply
  4. Please let me know if you’re looking for a article writer for your weblog.
    You have some really great articles and I believe I
    would be a good asset. If you ever want to take some of the load off, I’d absolutely love to write some material for your blog in exchange for
    a link back to mine. Please shoot me an e-mail if interested.
    Many thanks!

    Reply
  5. Hello would you mind sharing which blog platform you’re
    using? I’m going to start my own blog in the near future but I’m having a
    hard time selecting between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your layout seems different then most blogs and I’m looking for something
    completely unique. P.S Sorry for getting off-topic but
    I had to ask!

    Reply
  6. I just like the valuable info you supply for your articles.

    I will bookmark your weblog and check once more right here regularly.
    I’m rather certain I’ll learn many new stuff proper here!
    Good luck for the following!

    Reply

Leave a Comment