简体   繁体   English

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

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

I have a structure like below; 我的结构如下:

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',
      },
    }

};

and I would like to get all 'value' entries of user-x elements by filtering their 'property' values. 我想通过过滤用户“ x”元素的“属性”值来获取它们的所有“值”条目。

Eg. 例如。

I would like filter all 'name's of users (no matter in which device and what are those user-ids) based on their 'authority' property and get 'John Doe','Bill Doe','Jack Doe','Marry Doe' (as an array) if I would like filter for 'author1' 'authority' so I can get which users have 'author1' authorities on any devices. 我想根据用户的“ authority”属性过滤所有用户的“名称”(无论在哪个设备中以及这些用户ID是什么),并获取'John Doe','Bill Doe','Jack Doe','Marry Doe' (作为一个数组),如果我想过滤'author1''authority'以便我可以获取哪些用户在任何设备上都具有'author1'权限。

I've checked many places (including StackOverflow) but most of the examples are limited to two-dimensional object arrays, variables were specific or objects were based on an integer (like [0] => Array). 我检查了很多地方(包括StackOverflow),但是大多数示例都限于二维对象数组,变量是特定的,或者对象是基于整数的(例如[0] => Array)。

But in this example, 'device-x' and 'user-x' entries are uncertain (so I cannot say their values are these) but 'name' and 'authority' keys are certain (assign by the system) and the count of these variables can be varied (crud operations). 但是在此示例中, 'device-x''user-x'条目是不确定的(因此我不能说它们的值是这些),但是'name''authority'键是确定的(由系统分配),并且计数这些变量可以改变(粗操作)。

Thanks right now. 谢谢你

UPDATE : Because of my assumption error (I thought that if I write user-x parts different that each other, people think that these values are not follow any rule) question was not clear. 更新 :由于我的假设错误(我认为如果我编写的user-x部分彼此不同,人们会认为这些值不遵循任何规则),这个问题尚不清楚。 So I edited in code. 所以我在代码中进行了编辑。 Finally : owners of 'name' and 'authority' key-value pairs are user names and they are user defined. 最后:“名称”和“授权”键值对的所有者是用户名,并且是用户定义的。

So, all device objects will have id, template, unknown-user-field, but all unknown-user-fields must have 'name' and 'authority' key-value pairs. 因此,所有设备对象将具有ID,模板,未知用户字段,但是所有未知用户字段都必须具有“名称”和“授权”键值对。

You can use for-in loop to traverse an object. 您可以使用for-in循环遍历对象。 Following ways could achieve your need 以下方式可以满足您的需求

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)

Using reduce & filter & map . 使用reducefiltermap

Updated : I've added a isLikeUserObj function that probs for name & authority fields. 更新 :我添加了一个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