簡體   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