简体   繁体   中英

How can I gain access to the repeating element data inside of a named object inside of an object without mentioning the name in javascript?

I am trying to gain access of the objects that are inside of a object, but the problem is, I wouldn't know the names of the key object, For example:

{
  is7MfqpPY2UtoTfrS0rKt386GCc2:{
    DeviceModal:"djfiasdf"
    DeviceName:"276362"
    DeviceType:"Type2"
    Job:"Plumber"
    Latitude:40.7579067
    Longitude:-73.9726483
    Request:"jdhfjasdfjsdjl"
    userID:"is7MfqpPY2UtoTfrS0rKt386GCc2"
  },
  8sdfasdhfu8ewuhsdfwefs:{
    DeviceModal:"djfiasdf"
    DeviceName:"276362"
    DeviceType:"Type2"
    Job:"Plumber"
    Latitude:40.7579067
    Longitude:-73.9726483
    Request:"jdhfjasdfjsdjl"
    userID:"is7MfqpPY2UtoTfrS0rKt386GCc2"
  }
}

In the above object, you can see there are two random unique keys, the keys have repeating values, all I want to do is that, I wanna get the values inside of those repeating keys, but I don't want to mention the random unique key...Please tell me if there is any way to do that in Javascript?

var a = {
  is7MfqpPY2UtoTfrS0rKt386GCc2:{
    DeviceModal:"djfiasdf"
    DeviceName:"276362"
    DeviceType:"Type2"
    Job:"Plumber"
    Latitude:40.7579067
    Longitude:-73.9726483
    Request:"jdhfjasdfjsdjl"
    userID:"is7MfqpPY2UtoTfrS0rKt386GCc2"
  },
  8sdfasdhfu8ewuhsdfwefs:{
    DeviceModal:"djfiasdf"
    DeviceName:"276362"
    DeviceType:"Type2"
    Job:"Plumber"
    Latitude:40.7579067
    Longitude:-73.9726483
    Request:"jdhfjasdfjsdjl"
    userID:"is7MfqpPY2UtoTfrS0rKt386GCc2"
  }
}

Object.values( a ); // This will give you all values

// Example:

Object.values( a ).foreach( ( value ) => {
    console.log( value );
}

Object.keys( a ); // This will give you all keys

// Example:

Object.keys( a ).foreach( ( key ) => {
 console.log(a[key]); // Will console the value for this key
}

// If you wanted to go even further and get all the values of the values object you could do 

Object.values( a ).foreach( ( value ) => {
 Object.keys(value).foreach( ( key ) => { 
   console.log(`${key} - ${value[key]}`);
});
}

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