Accessing something inside the object when you don

2020-06-07 12:03发布

I am getting a following object

{
  IuW1zvaSABwH4q: {
    label: 'Random Image of TypeScript not relavent to coworking',
    thumbId: 'd501-f-b601-c8b1-4bd995e',
    schemaType: 'xman-assets-image-set'
  }
}

Now, I want to access the value of thumbID inside it i.e. d501-f-b601-c8b1-4bd995e

But my root key seems to be dynamic/random (IuW1zvaSABwH4q), How can I access the value inside it?

标签: javascript
8条回答
冷血范
2楼-- · 2020-06-07 12:10

An alternative way of using a combination of

JSON.stringify() and split()

const obj = {
  IuW1zvaSABwH4q: {
    label: 'Random Image of TypeScript not relavent to coworking',
    thumbId: 'd501-f-b601-c8b1-4bd995e',
    schemaType: 'xman-assets-image-set'
  }
}
let thumbId = JSON.stringify(obj).split('"thumbId":')[1].split('"')[1].split('"')[0]
console.log(thumbId)

查看更多
一夜七次
3楼-- · 2020-06-07 12:11

You can use for..in to iterate the object then check if this object has a key by name thumbId. This check will ensure that code does not throw error if the object does not have thumbId key

let obj = {
  IuW1zvaSABwH4q: {
    label: 'Random Image of TypeScript not relavent to coworking',
    thumbId: 'd501-f-b601-c8b1-4bd995e',
    schemaType: 'xman-assets-image-set'
  }
}


for (let keys in obj) {
  if (obj[keys].hasOwnProperty('thumbId')) {
    console.log(obj[keys].thumbId);
  }
}

查看更多
不美不萌又怎样
4楼-- · 2020-06-07 12:14

var json = {
  IuW1zvaSABwH4q: {
    label: 'Random Image of TypeScript not relavent to coworking',
    thumbId: 'd501-f-b601-c8b1-4bd995e',
    schemaType: 'xman-assets-image-set'
  },
    IuW2zvaSABwH4q: {
    label: 'Random Image of TypeScript not relavent to coworking',
    thumbId: 'd501-f-b601-c8b1-4bd995e',
    schemaType: 'xman-assets-image-set'
  },
    IuW3zvaSABwH4q: {
    label: 'Random Image of TypeScript not relavent to coworking',
    thumbId: 'd501-f-b601-c8b1-4bd995e',
    schemaType: 'xman-assets-image-set'
  }
}

for (var key in json) {
  if (json[key]) {
    for (var x in json[key]) {
      if (x === 'thumbId') console.log(json[key][x]);
    }
  }
}

I use two nested for to extract the thumbId.

查看更多
做自己的国王
5楼-- · 2020-06-07 12:17

Assuming there is only one property you could access it via the first property.

const obj = { IuW1zvaSABwH4q: 
      { label: 'Random Image of TypeScript not relavent to coworking', thumbId: 'd501-f-b601-c8b1-4bd995e', schemaType: 'xman-assets-image-set' 
       } 
    };
    
console.log(obj[Object.getOwnPropertyNames(obj)[0]].thumbId);

查看更多
Fickle 薄情
6楼-- · 2020-06-07 12:25

You can use Array.map to transform it and Array.forEach to get it and print it in the console.

const obj = {
    IuW1zvaSABwH4q: {
        label: 'Random Image of TypeScript not relavent to coworking',
        thumbId: 'd501-f-b601-c8b1-4bd995e',
        schemaType: 'xman-assets-image-set'
    },
    YuW1zvaSABwH7q: {
        label: 'Random Image of TypeScript not relavent to coworking',
        thumbId: 'as90-f-b601-c8b1-4bd9958', 
        schemaType: 'xman-assets-image-set'
    }
};
//for one object
console.log(Object.values(obj)[0].thumbId);
//multiple unknown keys
Object.values(obj).map(ele => ele.thumbId).forEach(th=> console.log(th));

查看更多
趁早两清
7楼-- · 2020-06-07 12:26

You can get the values from object and than access the desired key.

let obj =  {
    IuW1zvaSABwH4q: 
      {
        label: 'Random Image of TypeScript not relavent to coworking', 
        thumbId: 'd501-f-b601-c8b1-4bd995e',
        schemaType: 'xman-assets-image-set' 
      } 
    }
    
let op = Object.values(obj)[0].thumbId

console.log(op)

查看更多
登录 后发表回答