Javascript对象按属性获取对象

时间:2019-03-02 14:30:29

标签: javascript vue.js ecmascript-6 ecmascript-5

我有2个用户的对象,如下所示。该对象只能包含2个用户。

{
   "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,如何在不知道其ID的情况下获取其他用户的名字?

6 个答案:

答案 0 :(得分:1)

使用Object.keys,您可以获得一个键数组:

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

知道数组将只包含两个项目和“ other”键,您可以使用Array.prototype.find来获取其他项目:

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

请记住,对象键始终是字符串,因此您可能需要调整ID的过滤和操作,以将其视为(或强制转换为)数字。

答案 1 :(得分:0)

您可以使用Object.keys访问值以获取ID。然后filter()获得其他用户

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 :(得分: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);

答案 3 :(得分:0)

您可以通过以下代码来实现。假设obj是对象,而id变量存储id。现在,使用Object.keys获取键(数组)并基于!== id过滤出结果。使用otherId获取相关的对象和名称。

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)

答案 4 :(得分:0)

您可以使用Object.keys()获取ID并对其进行过滤。

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

答案 5 :(得分:0)

您可以使用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)