Javascript对象按属性获取对象

问题描述 投票:0回答:6

我有一个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的情况下获取其他用户的名称?

javascript vue.js ecmascript-6 ecmascript-5
6个回答
1
投票

使用Object.keys,您可以获得一系列密钥:

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

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

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

请记住,对象键始终是字符串,因此您可能希望以对待(或强制转换)数字的方式调整ID上的过滤和操作。


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

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

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

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)

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)
© www.soinside.com 2019 - 2024. All rights reserved.