简体   繁体   中英

How to search through array of nested objects using Javascript or Lodash

I have an array of objects as follows:

const data = [
  {
    slug: "home",
    content_one: [],
    content_two: [
      {
        title: "Some title",
        description: 'Some description'
      }
    ],
    content_three: [
      {
        main_title: "Some title",
        content: 'Here comes some content'
      }
    ],
  },
  {
    slug: "contact",
    content_one: [
      {
        title: "Some title",
        description: 'Some description'
      }
    ],
    content_two: [],
    content_three: [],
  },
  {
    slug: "about-us",
    content_one: [],
    content_two: [],
    content_three: [],
  }
]

And I have an array of search terms as follows:

const search_terms = ['Some', 'title']

I want to search through the data array, and find all objects which have one of those searched terms.

The result should be as follows:

const res = [
    {
    slug: 'home',
    text: 'Some title',
    count: 2
  },
  {
    slug: 'contact',
    text: 'Some title',
    count: 1
  }
]

I'm not sure how to do that, because the properties of the nested arrays are not the same.

It needs to be something as follows:

let result = []
search_terms.map(term => {
    data.filter(item => {
    console.log(item)
  })
})

But not sure how to do that.

Here is the fiddle.

Any idea?

You could first find all objects with matching text using recursion and then do the count and remove duplicates.

 const data = [{"slug":"home","content_one":[],"content_two":[{"title":"Some title","description":"Some description"}],"content_three":[{"main_title":"Some title","content":"Here comes some content"}]},{"slug":"contact","content_one":[{"title":"Some title","description":"Some description"}],"content_two":[],"content_three":[]},{"slug":"about-us","content_one":[],"content_two":[],"content_three":[]}] const search_terms = ['Some', 'title'] function search(data, terms, slug = '') { const result = []; if (data.slug) { slug = data.slug; } if (Array.isArray(data)) { data.forEach(e => result.push(...search(e, terms, slug))) } else { let added = false; for (let i in data) { if (typeof data[i] == 'object') { result.push(...search(data[i], terms, slug)); } else { const check = terms.some(t => data[i].toLowerCase().includes(t.toLowerCase())); if (check && !added) { result.push({ slug, text: data[i] }); added = true; } } } } return result; } function uniq(data) { return data.reduce((r, e) => { const match = r.find(({ count, ...rest }) => _.isEqual(rest, e)); if (match) match.count++ else r.push({ ...e, count: 1 }); return r; }, []) } const array = search(data, search_terms); const result = uniq(array); console.log(result)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM