0

Considering this following javascript object below:

Object

{
    _id: "553ea677da00122e5413d8c2",
    {
        "labyname": "Mq",
        "data": [
            [
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
            ],
            [
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
            ], null, null, null, null, "Mq"]
    }: ""
}

To access id in that object I can do something like that:

var=object[0]["_id"]

Can anyone tell me how to access "labyname" and "data?

  • 2
    unfortunately what you posted isn't a valid object structure. There's either an extra set of `{}` after `_id` or a missing property name. Similar problem at the end. Need a clean and complete structure in order to assist – charlietfl Apr 27 '15 at 22:13
  • This is a maze value I am retrieving from a mongodb database. I get the value, I can access id, but before I wrote object["labyname"]["data"] it does not work – Andre Fauhlt Apr 27 '15 at 22:17
  • `data` and `labname` are properties on the same level – charlietfl Apr 27 '15 at 22:19
  • @charlietfl Unfortunately it might be valid JSON. See my answer as to how...but not why cause I have no idea why you would ever do that. – Jason Cust Apr 27 '15 at 22:31

1 Answers1

0

Something is very wrong with this object. It looks like what you have is an object with two properties: _id and then a JSON string that has an empty string for a value. If that is what you truly have (may the coding gods have mercy on your algorithms) then you would need to get the second key by filtering out the _id and then parsing the other key into an object. Something like:

var json = Object.keys(your_crazy_object).filter(function (key) { return key !== '_id'; })[0];
var obj = JSON.parse(json);
console.log(obj.labyname); // "Mq"
console.log(obj.data); // [[[1,1,1,...,1,1,1]], [[1,1,1,...,1,1,1]], null, null, null, null, "Mq"]
Jason Cust
  • 10,743
  • 2
  • 33
  • 45
  • this is truly what I find from mongodb collection. By the way, I will try your code and will tell you if I succeed! – Andre Fauhlt Apr 27 '15 at 22:59
  • If this is the issue you might want to check the function that is saving the document in that form. – Jason Cust Apr 27 '15 at 23:36
  • @AndreFauhlt I'm curious if that was the issue. Any updates? – Jason Cust Apr 28 '15 at 15:51
  • Thank's for for the trial. But nothing works. httpresponse is the variable that returns those nested object. I did something like this instead in the following line: var json = Object.keys(your_crazy_object).filter(function (key) { return key !== '_id'; })[0]; var obj = JSON.parse(json); console.log(obj.labyname); console.log(obj.data); Unfortunately, it displays "undefined" – Andre Fauhlt Apr 28 '15 at 16:58
  • Can I put httpresponse between the parenthesis instead of "your-crazy_object)? – Andre Fauhlt Apr 28 '15 at 17:04
  • I modify something from the function by using the right variable name; as a resutl: IT WORKS!!! Thank you!!!! – Andre Fauhlt Apr 28 '15 at 17:09
  • `your-crazy_object` was meant to be whatever the object you included in your question. It's just a variable but should be set to the object in question. – Jason Cust Apr 28 '15 at 17:09
  • Yes! it works! Unfortunately I cannot vote up for you! – Andre Fauhlt Apr 28 '15 at 17:12