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

Javascript通过其他值从多维数组对象获取值,javascript,arrays,object,multidimensional-array,Javascript,Arrays,Object,Multidimensional Array,我有一个如下的结构 var devices = { 'device-1' : { 'id' :'device1', 'template' :'template-1', 'user-1' :{ 'name' : 'John Doe', 'authority' : 'author1', }, 'admin-1' :{ 'name' : 'Bob Doe', 'autho

我有一个如下的结构

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

};
我想通过过滤user-x元素的“属性”值来获取它们的所有“值”条目

例如

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

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

但是在这个例子中,
'device-x'
'user-x'
条目是不确定的(所以我不能说它们的值就是这些),但是
'name'
'authority'
键是确定的(由系统分配),并且这些变量的计数可以变化(crud操作)

现在就谢谢你

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


因此,所有设备对象都将具有id、模板、未知用户字段,但所有未知用户字段都必须具有“name”和“authority”键值对。

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

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)

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

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)

使用
reduce
&
过滤器
&
map

已更新:我添加了一个
isLikeUserObj
函数,用于探测
名称
权限
字段

const设备={
“设备-1”:{
'id':'device1',
“模板”:“模板-1”,
“用户1”:{
“姓名”:“约翰·多伊”,
“authority”:“author1”,
},
“管理员-1”:{
'姓名':'鲍勃·多伊',
“authority”:“author2”,
},
“用户-35”:{
“姓名”:“比尔·多伊”,
“authority”:“author1”,
},
“作者-42”:{
“姓名”:“杰克·多伊”,
“authority”:“author1 | author3”,
}
},
“设备-2”:{
'id':'device2',
“模板”:“模板-2”,
‘some-27’:{
“姓名”:“约翰·多伊”,
“authority”:“author1”,
},
“其他-42”:{
“姓名”:“杰克·多伊”,
“authority”:“author2”,
}
},
“设备-7”:{
'id':'device7',
“模板”:“模板-1”,
“用户2”:{
“姓名”:“萨曼莎·多伊”,
“authority”:“author2”,
},
“管理员-40”:{
“姓名”:“玛丽·多伊”,
“authority”:“author1”,
},
}
};
const result=getUserByAuthority('author3');
函数getUserByAuthority(requiredAuth){
返回Object.keys(设备).reduce((结果,设备键)=>{
const users=Object.key(设备[deviceKey])
.filter((key)=>isUserLikeObj(设备[deviceKey][key]))
.map(userKey=>devices[deviceKey][userKey])
.filter((user)=>user.authority.split(“|”).indexOf(requiredAuth)>-1)
.map((用户)=>user.name)
返回result.concat(用户);
}, [])
}
函数isUserLikeObj(值){
返回typeof value==='object'&&value.hasOwnProperty('name')&&value.hasOwnProperty('authority'))
}

console.log(结果)
使用
reduce
&
过滤器
&
映射

已更新:我添加了一个
isLikeUserObj
函数,用于探测
名称
权限
字段

const设备={
“设备-1”:{
'id':'device1',
“模板”:“模板-1”,
“用户1”:{
“姓名”:“约翰·多伊”,
“authority”:“author1”,
},
“管理员-1”:{
'姓名':'鲍勃·多伊',
“authority”:“author2”,
},
“用户-35”:{
“姓名”:“比尔·多伊”,
“authority”:“author1”,
},
“作者-42”:{
“姓名”:“杰克·多伊”,
“authority”:“author1 | author3”,
}
},
“设备-2”:{
'id':'device2',
“模板”:“模板-2”,
‘some-27’:{
“姓名”:“约翰·多伊”,
“authority”:“author1”,
},
“其他-42”:{
“姓名”:“杰克·多伊”,
“authority”:“author2”,
}
},
“设备-7”:{
'id':'device7',
“模板”:“模板-1”,
“用户2”:{
“姓名”:“萨曼莎·多伊”,
“authority”:“author2”,
},
“管理员-40”:{
“姓名”:“玛丽·多伊”,
“authority”:“author1”,
},
}
};
const result=getUserByAuthority('author3');
函数getUserByAuthority(requiredAuth){
返回Object.keys(设备).reduce((结果,设备键)=>{
const users=Object.key(设备[deviceKey])
.filter((key)=>isUserLikeObj(设备[deviceKey][key]))
.map(userKey=>devices[deviceKey][userKey])
.filter((user)=>user.authority.split(“|”).indexOf(requiredAuth)>-1)
.map((用户)=>user.name)
返回result.concat(用户);
}, [])
}
函数isUserLikeObj(值){
返回typeof value==='object'&&value.hasOwnProperty('name')&&value.hasOwnProperty('authority'))
}

console.log(result)
您的代码在哪里?我正在尝试使用此选项:但尚未成功。因为,虽然引用的QA是基于二维数组的,但我无法实现子变量。您的代码在哪里?我正在尝试使用它:但还不能成功。因为,虽然引用的QA基于二维数组,但我无法实现子变量。感谢您的回答,请检查问题更新设备对象是否包含
id
模板
未知用户字段
?是的,完全正确。但是所有未知用户字段都必须有“name”和“authority”键值对。更新了答案谢谢你的回答,请检查问题updateArea设备对象将包含