0

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?

1
  • Please do not post image.....instead post code. Commented Mar 2, 2019 at 14:32

6 Answers 6

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Comments

0

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

2 Comments

But what it the order of the users in the object change?
@itsliamoco sorry I think i misunderstood. I updated
0

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)

Comments

0

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)

1 Comment

Note that otherId is an array: [ "71" ] and not 71. It works probably because [ "71" ].toString() is 71. You should use find instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.