1

I am pretty new to learning to code. So sorry if this is a stupid question.

I have a nested object database that I want to search for a character name and then return to me who's character it is. But, so far, I can only find solutions that search the top level objects or are for arrays and I am running out of ideas. Is it possible to search in depth for a name like 'Farah' and then somehow get 'olis characters' back?

Thanks in advance for any advice you guys might have!

{
  "olis characters": {
    "0": {
      "name": "Farah",
      "class": "rogue",
      "level": 74
    },
    "1": {
      "name": "Grop",
      "class": "paladin",
      "level": 31
    },
    "2": {
      "name": "Skolmr",
      "class": "druid",
      "level": 85,
    }
  },
  "chris characters": {
    "0": {
      "name": "Trygve",
      "class": "bard",
      "level": 28
    },
    "1": {
      "name": "Brusi",
      "class": "rogue",
      "level": 10
    },
    "2": {
      "name": "Steini",
      "class": "skald",
      "level": 58
    }
  }
}


xKobalt
  • 1,498
  • 2
  • 13
  • 19
lurf
  • 23
  • 2
  • Are the names unique? – Mark Mar 06 '20 at 22:53
  • Does this answer your question? [Search a JavaScript object](https://stackoverflow.com/questions/1820593/search-a-javascript-object) – zfrisch Mar 06 '20 at 22:57
  • The datastucture you have is a little strange. Do you really have an object with keys like `0` and `1` or is that an array? – Mark Mar 06 '20 at 23:12
  • @MarkMeyer yes the names are unique. And i am sure it's a big object and not an array. I know the data structure looks a little odd. Would it help if i changed the numbers to the character names somehow? It's still a nested object then that i have trouble searching through.. hmm – lurf Mar 06 '20 at 23:37
  • @lurf if the names are unique, you could consider a slightly different data format, where instead of keys like `0`, and `1`, you use the names for keys, then you could test for inclusion without iterating like: `if ('Trygve ' in data['olis characters'])`. – Mark Mar 06 '20 at 23:39
  • @zfrisch thanks for the reply! I had a look and the question is 10 years old. Didn't Javascript undergo a huge overhaul a few years ago? Is that information still relevant anyway? – lurf Mar 06 '20 at 23:41
  • There are better things. Thing is, iterating through an object while keeping a reference to a parent has had pretty much the same solution for eons. Here's a part solution that I'll revise for you later. It handles navigating an object, but it needs a search function https://codepen.io/zfrisch/pen/dLeqYd?editors=1010 – zfrisch Mar 06 '20 at 23:58

2 Answers2

0

As it is, your data is a little odd. You have and object with numeric keys, which suggests it should be an array. Having said that you can still search through the Object.values to get the data you want.

let data = {"olis characters": {"0": {"name": "Farah","class": "rogue","level": 74},"1": {"name": "Grop","class": "paladin","level": 31},"2": {"name": "Skolmr","class": "druid","level": 85,}},"chris characters": {"0": {"name": "Trygve","class": "bard","level": 28},"1": {"name": "Brusi","class": "rogue","level": 10},"2": {"name": "Steini","class": "skald","level": 58}}}


function findChar(name, data) {
  for (let charGroup of Object.values(data)) {
    let found = Object.values(charGroup).find(char => char.name === name)
    if (found) return found
  }
}

console.log(findChar('Grop', data))
console.log(findChar('Brusi', data))

// will return undefined if the name is not there:
console.log(findChar('Mark', data))

If you changed the data model to a simple array like:

let data = {
    "olis characters": [{
        "name": "Farah",
        "class": "rogue",
        "level": 74
      },
      {
        "name": "Grop",
        "class": "paladin",
        "level": 31
      }
    ],
    "chris characters": [{
        "name": "Trygve",
        "class": "bard",
        "level": 28
      },
      // ...
    ]
 }

...you could avoid one of those Object.values and use find directly.

function findChar(name, data){
   for (let charGroup of Object.values(data)){
      let found = charGroup.find(char => char.name === name)
      if (found) return found
   }
 }
Mark
  • 90,562
  • 7
  • 108
  • 148
  • this was a HUGE help! Thank you so much! I changed the database to something like you suggested and now everything works. You are the best! – lurf Mar 07 '20 at 16:50
-1

It gets harder to run a loop through an object like this, maybe an object of objects is not the right data structure, consider using an array of object instead.

But May be this code can help you

const data = {
    "olis characters": {
        "0": {
            "name": "Farah",
            "class": "rogue",
            "level": 74
        },
        "1": {
            "name": "Grop",
            "class": "paladin",
            "level": 31
        },
        "2": {
            "name": "Skolmr",
            "class": "druid",
            "level": 85,
        }
    },
    "chris characters": {
        "0": {
            "name": "Trygve",
            "class": "bard",
            "level": 28
        },
        "1": {
            "name": "Brusi",
            "class": "rogue",
            "level": 10
        },
        "2": {
            "name": "Steini",
            "class": "skald",
            "level": 58
        }
    }
}


Object.keys(data).forEach(item => {
    if(findItemByName('Farah', data[item])){
        console.log(item)
    }
})

function findItemByName(name, item) {
    let r = false;
    Object.keys(item).forEach(obj => {
        if (item[obj].name == name) {
            r = true;
        }

    });

    return r;
}
  • 1
    This answer is not an answer. At best it's a comment. – zfrisch Mar 06 '20 at 22:57
  • I understand where you're coming from and I'm not being aggressive about it or anything, but saying "go study *x*" and linking off-site literally does not qualify as appropriate content for an answer. That's all. – zfrisch Mar 06 '20 at 23:04