itsliamoco
itsliamoco

Reputation: 1048

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?

Upvotes: 0

Views: 79

Answers (6)

Sunil Chaudhary
Sunil Chaudhary

Reputation: 4743

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)

Upvotes: 0

jaredrethman
jaredrethman

Reputation: 511

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)

Upvotes: 0

Maheer Ali
Maheer Ali

Reputation: 36594

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);

Upvotes: 0

Sebastian Speitel
Sebastian Speitel

Reputation: 7346

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'));

Upvotes: 0

rishat
rishat

Reputation: 8376

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.

Upvotes: 1

Anurag Awasthi
Anurag Awasthi

Reputation: 6233

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);

Upvotes: 0

Related Questions