简体   繁体   中英

Javascript Object get object by property

I have an object of 2 users that looks like the following. The Object will only ever contain 2 users.

{
   "71":{
      "avatarURL":"__vue_devtool_undefined__",
      "createdAt":"2018-10-13T16:05:19Z",
      "customData":"__vue_devtool_undefined__",
      "id":"71",
      "name":"Angeline Fadel",
      "updatedAt":"2018-10-13T16:05:19Z",
      "presenceStore":{
         "71":"online"
      }
   },
   "199":{
      "avatarURL":"__vue_devtool_undefined__",
      "createdAt":"2018-10-13T16:06:13Z",
      "customData":"__vue_devtool_undefined__",
      "id":"199",
      "name":"Rodrigo Schuster",
      "updatedAt":"2018-10-13T16:06:13Z",
      "presenceStore":{
         "71":"online"
      }
   }
}

Let's say my user ID is 199, how to I get the name of the other user without knowing it's ID?

With Object.keys , you can get an array of keys:

const users = { 199: {...}, 71: {...} };
const ids = Object.keys(users); // -> ['199', '71']

Knowing that the array will only contain two items and the "other" key, you might use Array.prototype.find to get the other item:

const myId = '199';
const targetId = ids.find(id => id !== myId); // -> '71'

Remember that object keys are always strings, so you may want to tweak the filtering and operations on IDs in a way that they are treated as (or coerced into) numbers.

You can access the values using Object.keys to get ids. And then filter() to get other user

 let users = { '71':{name:"First User"}, '199':{name:"Second User"} } let id = '199' let otherId = Object.keys(users).filter(key => key !== id)[0] console.log(users[otherId].name); 

 var obj={ 199: { name: "abc" }, 71: { name: "def" } } var knownKey = 199; var otherKey = Object.keys(obj).filter(key => key != knownKey).pop(); console.log("name = " + obj[otherKey].name); 

You can achieve it by following code. Suppose obj is the object and id variable stores the id. Now, use Object.keys to get the keys (which is array) and filter out the result based on !== id . Use otherId to get the relevant object and name.

 obj = { "71":{ "avatarURL":"__vue_devtool_undefined__", "createdAt":"2018-10-13T16:05:19Z", "customData":"__vue_devtool_undefined__", "id":"71", "name":"Angeline Fadel", "updatedAt":"2018-10-13T16:05:19Z", "presenceStore":{ "71":"online" } }, "199":{ "avatarURL":"__vue_devtool_undefined__", "createdAt":"2018-10-13T16:06:13Z", "customData":"__vue_devtool_undefined__", "id":"199", "name":"Rodrigo Schuster", "updatedAt":"2018-10-13T16:06:13Z", "presenceStore":{ "71":"online" } } } id = "199" otherId = Object.keys(obj).find(data => data !== id) result = obj[otherId].name alert(result) 

You could get the ids using Object.keys() and filter them.

 const usrs = { '1': { name: 'a' }, '2': { name: 'b' } }; function other(usrs, id) { const allId = Object.keys(usrs); console.log('allId:', allId); const otherId = allId.filter(k => k !== id); console.log('otherId:', otherId); const otherUser = otherId.map(uid => usrs[uid]); console.log('otherUser:', otherUser); const otherNames = otherUser.map(u => u.name); return otherNames } console.log(other(usrs, '1')); 

You could use delete

 const users = { "71":{ "avatarURL":"__vue_devtool_undefined__", "createdAt":"2018-10-13T16:05:19Z", "customData":"__vue_devtool_undefined__", "id":"71", "name":"Angeline Fadel", "updatedAt":"2018-10-13T16:05:19Z", "presenceStore":{ "71":"online" } }, "199":{ "avatarURL":"__vue_devtool_undefined__", "createdAt":"2018-10-13T16:06:13Z", "customData":"__vue_devtool_undefined__", "id":"199", "name":"Rodrigo Schuster", "updatedAt":"2018-10-13T16:06:13Z", "presenceStore":{ "71":"online" } } }; const knowUserId = '199'; const knowUserIndex = Object.keys(users).indexOf(knowUserId); if(knowUserIndex > -1){ delete users[knowUserId]; } console.log(users) 

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