DEV Community

Md. Khalid Hossen
Md. Khalid Hossen

Posted on

2 2

Matching search Text become small like google suggestions in Js

Making pain Text match data normal text and others become bold

 let term = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,';
      let search1 = 'Lorem';
      let parts = term.split(search);
      console.log("parts", parts)
      return parts.map((item, i) => (
        <>{item && <strong>{item}</strong>}{(i !== parts.length - 1) ? search1 : null}</>
      ))
Enter fullscreen mode Exit fullscreen mode

*Array of Array match Make suggest text bold matching with search text *

let termData = [
       { "title": "apple orange banana" },
       { "title": "upload app koro na kn???" },
        { "title": "range ban apple ana" },
        { "title": "Hello world" },
        { "title": "Hello no world" },
    ];
    let search = 'app';
    const matchData = termData?.filter((product) => product.title.toLowerCase().includes(search.toLowerCase()));

    let newArray = [];

    matchData.map(item => {
        return newArray.push(item.title.split(search))
    })

    return newArray.map((item, index) => (
        <>
            {item.map((second, i) => (
                <span onClick={() => alert(`"hey MR.", ${index}`)}>{second && <strong>{second}</strong>}{(i !== item.length - 1) ? search : null}</span>
            ))}
            <br />
        </>
    ))
Enter fullscreen mode Exit fullscreen mode

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay