Home > Mobile >  Search substring in a object of a array of objects Javascript
Search substring in a object of a array of objects Javascript

Time:12-26

I have a array of objects. In object, I have string value in each object. I have to search specific substring in the string.

[
    {
        "link": "https://www.sec.gov/Archives/edgar/data/1702510/000170251022000084/0001702510-22-000084-index.htm",
        "summary": "\n <b>Filed:</b> 2022-12-23 <b>AccNo:</b> 0001702510-22-000084 <b>Size:</b> 25 KB\n<br>**Item 3.02**: Unregistered Sales of Equity Securities\n<br>Item 7.01: Regulation FD Disclosure\n"
    },
    {
        "link": "https://www.sec.gov/Archives/edgar/data/1853774/000182912622020524/0001829126-22-020524-index.htm",
        "summary": "\n <b>Filed:</b> 2022-12-23 <b>AccNo:</b> 0001829126-22-020524 <b>Size:</b> 940 KB\n<br>**Item 1.01**: Entry into a Material Definitive Agreement\n<br>Item 9.01: Financial Statements and Exhibits\n"
    },
    {
        "link": "https://www.sec.gov/Archives/edgar/data/1859007/000149315222036510/0001493152-22-036510-index.htm",
        "summary": "\n <b>Filed:</b> 2022-12-23 <b>AccNo:</b> 0001493152-22-036510 <b>Size:</b> 278 KB\n<br>**Item 1.01**: Entry into a Material Definitive Agreement\n<br>Item 9.01: Financial Statements and Exhibits\n"
    },
....
]

I have search substring like "Item 3.02" in summary value. if the substring found get object and return array of object that contains substring value

I have tried tried match and includes methods

CodePudding user response:

You need to iterate over all the items in the array and then use the includes string method to search for the substring.

const data = [
  {
      "link": "...",
      "summary": "\n <b>Filed:</b> 2022-12-23 <b>AccNo:</b> 0001702510-22-000084 <b>Size:</b> 25 KB\n<br>**Item 3.02**: Unregistered Sales of Equity Securities\n<br>Item 7.01: Regulation FD Disclosure\n"
  },
  {
      "link": "...",
      "summary": "..."
  },
]

function findItems(searchString) {
  return data.filter((item) => item.summary.includes(searchString));
}

const items = findItems("Item 3.02")
console.log(items);

CodePudding user response:

You need to traverse the array and string search in the object. Here's the same code:

const data = [
    {
        "link": "https://www.sec.gov/Archives/edgar/data/1702510/000170251022000084/0001702510-22-000084-index.htm",
        "summary": "\n <b>Filed:</b> 2022-12-23 <b>AccNo:</b> 0001702510-22-000084 <b>Size:</b> 25 KB\n<br>**Item 3.02**: Unregistered Sales of Equity Securities\n<br>Item 7.01: Regulation FD Disclosure\n"
    },
    {
        "link": "https://www.sec.gov/Archives/edgar/data/1853774/000182912622020524/0001829126-22-020524-index.htm",
        "summary": "\n <b>Filed:</b> 2022-12-23 <b>AccNo:</b> 0001829126-22-020524 <b>Size:</b> 940 KB\n<br>**Item 1.01**: Entry into a Material Definitive Agreement\n<br>Item 9.01: Financial Statements and Exhibits\n"
    },
    {
        "link": "https://www.sec.gov/Archives/edgar/data/1859007/000149315222036510/0001493152-22-036510-index.htm",
        "summary": "\n <b>Filed:</b> 2022-12-23 <b>AccNo:</b> 0001493152-22-036510 <b>Size:</b> 278 KB\n<br>**Item 1.01**: Entry into a Material Definitive Agreement\n<br>Item 9.01: Financial Statements and Exhibits\n"
    }
]

const textToSearch = "Item 7.01";

const filteredItems = data.filter((item) => {
  return item.summary.includes(textToSearch);
});

console.log(filteredItems);

  • Related