简体   繁体   中英

Access the property of a object inside a object

I need to pull the value of property "title"

KV = {
      clientPath: '/0000000000/client',
      serverPath: '',
      application: '/00000000/client/application/player.js',
      properties: '/000000000/client/custom-config/AppProperties.js',
      pollingEnabled: false,
      customerConfig: {},
      presentationTypeConfig: {},
      kuluConfig: {},
      kulu: {
             "guid" : "XXXXXXX",
             "title" : "XXXXX",
             "createdInApp" : false,
             "allowFeedback" : true,
             "publisher" : {
             "id" : 000000001,
             "username" : "XXXXXXX",
             "name" : "XXXXXXXX"
      },

I tried looping but I just get returned undefined.

I have no access to the code to change it.

你有尝试过吗?

KV.kulu.title

first of all: the json you have posted is invalid. publisher-property has only an opening curly bracket.

second: here's a working fiddle, with valid json and just the code kv.kulu.title which does exactly what you are (literally) asking for: http://jsfiddle.net/k75cxdkh/1/

edit: I'm just guessing here, but re-reading your question and json code, it seems you try to loop over an array of objects to get a nested object by it's value dynamically. When trying this, do it eg like this (using underscorejs):

var arr = _.filter(KV, function(obj) {
    return _.some(obj.kulu, {id: ID_TO_FIND});
});

if not, nevermind. It's just a bit weird you are asking for a such common task.

快速简便: KV.kulu.title无需循环!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM