繁体   English   中英

Javascript通过其他值从多维数组对象获取值

[英]Javascript get values from multidimensional array object by other values

我的结构如下:

var devices = {
    'device-1' : {
      'id' :'device1',
      'template' :'template-1',
      'user-1' :{
        'name' : 'John Doe',
        'authority' : 'author1',
      },
      'admin-1' :{
        'name' : 'Bob Doe',
        'authority' : 'author2',
      },
      'user-35' :{
        'name' : 'Bill Doe',
        'authority' : 'author1',
      },
      'author-42' :{
        'name' : 'Jack Doe',
        'authority' : 'author1|author3',
      }
  },
    'device-2' : {
      'id' :'device2',
      'template' :'template-2',
      'some-27' :{
        'name' : 'John Doe',
        'authority' : 'author1',
      },
      'other-42' :{
        'name' : 'Jack Doe',
        'authority' : 'author2',
      }
  },
    'device-7' : {
      'id' :'device7',
      'template' :'template-1',
      'user-2' :{
        'name' : 'Samantha Doe',
        'authority' : 'author2',
      }
      'admin-40' :{
        'name' : 'Marry Doe',
        'authority' : 'author1',
      },
    }

};

我想通过过滤用户“ x”元素的“属性”值来获取它们的所有“值”条目。

例如。

我想根据用户的“ authority”属性过滤所有用户的“名称”(无论在哪个设备中以及这些用户ID是什么),并获取'John Doe','Bill Doe','Jack Doe','Marry Doe' (作为一个数组),如果我想过滤'author1''authority'以便我可以获取哪些用户在任何设备上都具有'author1'权限。

我检查了很多地方(包括StackOverflow),但是大多数示例都限于二维对象数组,变量是特定的,或者对象是基于整数的(例如[0] => Array)。

但是在此示例中, 'device-x''user-x'条目是不确定的(因此我不能说它们的值是这些),但是'name''authority'键是确定的(由系统分配),并且计数这些变量可以改变(粗操作)。

谢谢你

更新 :由于我的假设错误(我认为如果我编写的user-x部分彼此不同,人们会认为这些值不遵循任何规则),这个问题尚不清楚。 所以我在代码中进行了编辑。 最后:“名称”和“授权”键值对的所有者是用户名,并且是用户定义的。

因此,所有设备对象将具有ID,模板,未知用户字段,但是所有未知用户字段都必须具有“名称”和“授权”键值对。

您可以使用for-in循环遍历对象。 以下方式可以满足您的需求

const result = []

for (let i in devices) {
  for (let j in devices[i]) {
    if (/user-\d+/.test(j)) {
      if (devices[i][j].authority.split('|').indexOf('author1') !== -1) {
        result.push(devices[i][j].name)
      }
    }
  }
}

console.log(result)

使用reducefiltermap

更新 :我添加了一个isLikeUserObj函数,该函数可能会显示nameauthority字段。

 const devices = { 'device-1': { 'id': 'device1', 'template': 'template-1', 'user-1': { 'name': 'John Doe', 'authority': 'author1', }, 'admin-1': { 'name': 'Bob Doe', 'authority': 'author2', }, 'user-35': { 'name': 'Bill Doe', 'authority': 'author1', }, 'author-42': { 'name': 'Jack Doe', 'authority': 'author1|author3', } }, 'device-2': { 'id': 'device2', 'template': 'template-2', 'some-27': { 'name': 'John Doe', 'authority': 'author1', }, 'other-42': { 'name': 'Jack Doe', 'authority': 'author2', } }, 'device-7': { 'id': 'device7', 'template': 'template-1', 'user-2': { 'name': 'Samantha Doe', 'authority': 'author2', }, 'admin-40': { 'name': 'Marry Doe', 'authority': 'author1', }, } }; const result = getUserByAuthority('author3'); function getUserByAuthority(requiredAuth) { return Object.keys(devices).reduce((result, deviceKey) => { const users = Object.keys(devices[deviceKey]) .filter((key) => isUserLikeObj(devices[deviceKey][key])) .map(userKey => devices[deviceKey][userKey]) .filter((user) => user.authority.split('|').indexOf(requiredAuth) > -1) .map((user) => user.name) return result.concat(users); }, []) } function isUserLikeObj(value) { return typeof value === 'object' && value.hasOwnProperty('name') && value.hasOwnProperty('authority') } console.log(result) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM