0

I search on stackoverflow before post my question, but I didn't find any solution. I have an object like this :

"{"COURRIERS":
     {"05. Juridique":
         [{"res_id":100,"type_label":"Plainte","subject":"test23","doctypes_first_level_label":"COURRIERS","doctypes_second_level_label":"05. Juridique","folder_level":2}]
     }
}"

And I need to access it like an array, in order to get the information like res_id etc..

How can I do this ?

Thanks in advance

3
  • 2
    please add a wanted result. Commented Mar 8, 2017 at 15:27
  • 3
    That is string. To convert it to object use JSON.parse(string) then use Object.keys(obj) to get list of keys which can be used to get the array. Commented Mar 8, 2017 at 15:27
  • @Tushar When I display it with console.log I can see it's an object and not a string Commented Mar 8, 2017 at 15:37

3 Answers 3

2

Assuming that you won't have more than one object/array in each layer, this should get you what you need.

let obj = {
  "COURRIERS": {
    "05. Juridique": [{
      "res_id": 100,
      "type_label": "Plainte",
      "subject": "test23",
      "doctypes_first_level_label": "COURRIERS",
      "doctypes_second_level_label": "05. Juridique",
      "folder_level": 2
    }]
  }
}

let folder = Object.keys(obj)[0]
let type = Object.keys(obj[folder])[0]
let result = obj[folder][type][0]

console.log(result)

Sign up to request clarification or add additional context in comments.

Comments

1

You can gain access to the data in multiple ways. The following below will help clarify some of the way you can access some of the data.

myObj.type              = "Dot syntax";
myObj.type              = "Dot syntax";
myObj["date created"]   = "String with space";
myObj[str]              = "String value";
myObj[rand]             = "Random Number";
myObj[obj]              = "Object";
myObj[""]               = "Even an empty string";

For your problem you can use the following

var x = { 
 "COURRIERS":{
  "05. Juridique":[
     {
        "res_id":100,
        "type_label":"Plainte",
        "subject":"test23",
        "doctypes_first_level_label":"COURRIERS",
        "doctypes_second_level_label":"05. Juridique",
        "folder_level":2
     }
  ]
}};
console.log(x['COURRIERS']['05. Juridique'][0].res_id)

1 Comment

Thanks for you help. But as I answered to Mathieu Lescaudron, I have to access it with variable like this : data[folder][type][i]["res_id"]
1

Something like that ?

(I insert the data inside a variable and print the wanted result with key index)

let obj = {
   "COURRIERS":{
      "05. Juridique":[
         {
            "res_id":100,
            "type_label":"Plainte",
            "subject":"test23",
            "doctypes_first_level_label":"COURRIERS",
            "doctypes_second_level_label":"05. Juridique",
            "folder_level":2
         }
      ]
   }
};

console.log(obj["COURRIERS"]["05. Juridique"][0]["res_id"]);

EDIT

You want to acess it with variable. For avoid bug, I strong recommend you to check if the variable value key exist in the array/object like :

let folder = 'COURRIERS';

if(folder.indexOf(data) >= 0) { // folder.indexOf(data) = 0
// ... finish the job here :)
}
// indexOf return -1 if the value is not found

5 Comments

Yes, but I need to access it without string, but variable. Like data[folder][type][i]['res_id'] :/
it's the same, juste replace string with variable data[folder][type][i]["res_id"]
Well, as is, data[folder]... can't work because folder, type etc.. are not index of my object :/
let folder = "COURRIERS";, let type = "05. Juridique"; and access the object accordingly. Note that iis always 0 in this particular case. Access it as data[folder][type][0]["res_id"].
The thing you didn't understant is that the variable are not the same. Folder could be "COURRIERS" or "TOTO", you know what I mean ? This is why I need to convert it in array to have something like array['folder'] = "COURRIERS" or "TOTO" or... array['type'] = "05. Juridique" or.... etc etc... I don't know if it's clear enought. I have to access the res_id, subject etc attribute but without knowing the label of folder and type

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.