0

I would like to create a function that takes an object and returns true if the link id of the top level or any of the children matches a given ID. For example the link ID for Product paths is 51125095 so passing this ID into the function should result in true

Here is my sample object:

  const sampleObj = {
    id: '55259494',
    menuText: 'Top level link',
    link: {
      id: '55259472',
      slug: 'lop-level-link',
    },
    children: [
      {
        id: '53664310',
        menuText: 'Product paths',
        link: {
          id: '51125095',
          slug: 'product-paths',
        },
        children: [],
      },
      {
        id: '53664355',
        menuText: 'Testing',
        link: {
          id: '51272081',
          slug: 'testing',
        },
        children: [],
      },
      {
        id: '53664382',
        menuText: 'Relay',
        link: {
          id: '51489535',
          slug: 'relay',
        },
        children: [],
      },
      {
        id: '55259577',
        menuText: 'About us',
        link: {
          id: '55259487',
          slug: 'about-us',
        },
        children: [],
      },
    ],
  }

And here is the function I have started to build:

  const isObject = (value) => {
    return !!(value && typeof value === 'object' && !Array.isArray(value))
  }

  const containsActiveLink = (linkObject: any = {}, pageIDToMatch) => {
    if (isObject(linkObject)) {
      console.log('Run:' + linkObject.menuText)
      console.log(linkObject)
      if (linkObject.link.id === pageIDToMatch) {
        return true
      }

      if (linkObject.children.length > 0) {
        linkObject.children.map((child) => containsActiveLink(child, pageIDToMatch))
      }
    }
    return false
  }

  const isActiveLink = containsActiveLink(sampleObj, '51125095')

Any help will be greatly appreciated please

Hantastic
  • 3
  • 3

1 Answers1

0

Not the most efficient as it searches the entire object but that's a start.

const sampleObj={id:"55259494",menuText:"Top level link",link:{id:"55259472",slug:"lop-level-link"},children:[{id:"53664310",menuText:"Product paths",link:{id:"51125095",slug:"product-paths"},children:[]},{id:"53664355",menuText:"Testing",link:{id:"51272081",slug:"testing"},children:[]},{id:"53664382",menuText:"Relay",link:{id:"51489535",slug:"relay"},children:[]},{id:"55259577",menuText:"About us",link:{id:"55259487",slug:"about-us"},children:[]},]}

function find_id(data, id) {

  var found = false

  const iterate = (obj) => {
    if (!obj) {
      return;
    }
    Object.keys(obj).forEach(key => {
      var value = obj[key]
      if (key == 'id' && value == id) {
        found = true;
      }
      if (typeof value === "object" && value !== null) {
        iterate(value)
      }
    })
  }

  iterate(data)
  return found;
}
console.log(find_id(sampleObj, "55259577"))
IT goldman
  • 14,885
  • 2
  • 14
  • 28
  • Thanks so much. I will give this a go in my staging environment tomorrow. I really appreciate your help – Hantastic Aug 31 '22 at 20:06