0

I have a multidimensional array and each object has the same keys.

export const MENUS = [
    {
        "type": "main",
        "submenu": [],
        "path": "/a"
    },
    {
        "type": "main",
        "submenu": [
            {
                "type": "submenu",
                "submenu": [
                    {
                        "type": "submenu",
                        "submenu": [],
                        "path": "/b/4"
                    },
                ],
                "path": null
            },
            {
                "type": "submenu",
                "submenu": [],
                "path": "/b/1"
            }
        ],
        "path": null
    },
    {
        "type": "main",
        "submenu": [],
        "path": "/c"
    }
]

Now, I have a key and value (path: '/b/1') and I want to fetch parent object by key/value in array.

this is the result what I am looking for when I use { path: '/b/1' }.

{
        "type": "main",
        "submenu": [
            {
                "type": "submenu",
                "submenu": [
                    {
                        "type": "submenu",
                        "submenu": [],
                        "path": "/b/4"
                    },
                ],
                "path": null
            },
            {
                "type": "submenu",
                "submenu": [],
                "path": "/b/1"
            }
        ],
        "path": null
    },

If I use {path: '/c'}, then the result will be a root array. (equal to MENU) If anyone has a good solution, please advise me. Thanks.

7
  • 1
    Please edit your question to include what you've tried so far. Commented Apr 20, 2022 at 19:57
  • thanks for your reply, @Sean. I've just changed something. please let me know if it's clear. Commented Apr 20, 2022 at 19:59
  • Also note: this is not a multidimensional array. It's an array of objects. Commented Apr 20, 2022 at 19:59
  • Your edit did not add what you've tried so far. Commented Apr 20, 2022 at 20:00
  • oh. I am sorry. I changed the object data what I want to get. Commented Apr 20, 2022 at 20:02

1 Answer 1

1
function getObj(array, key, value) {
  return array.find(item => isThisItem(item))
  function isThisItem(current) {
    const entries = Object.entries(current)
    for (const [k, v] of entries) {
      if (k === key && v === value) return true
      if (Array.isArray(v)) {
        for (const f of v) {
          if (isThisItem(f)) return true
        }
      }
      if (typeof v === 'object' && v !== null) {
        if (isThisItem(v)) return true
      }
    }
  }
}

// usage
const obj = getObj(MENUS, 'path', '/b/1')
Sign up to request clarification or add additional context in comments.

2 Comments

Good answers include some explanation—not just code.
Awesome!. It is what I am looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.