【问题标题】:Javascript get values from multidimensional array object by other valuesJavascript通过其他值从多维数组对象中获取值
【发布时间】:2017-09-02 12:11:43
【问题描述】:

我有如下结构;

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 元素的所有“值”条目。

例如。

我想根据他们的“权限”属性过滤所有“用户名”(无论在哪个设备中以及那些用户 ID 是什么),如果我想过滤“ author1' 'authority' 所以我可以在任何设备上获取哪些用户拥有 'author1' 权限。

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

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

谢谢。

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

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

【问题讨论】:

  • 你的代码在哪里?
  • 我正在尝试使用这个:stackoverflow.com/questions/2722159/…,但还没有成功。因为,虽然引用的 QA 是基于二维数组的,但我无法实现子变量。

标签: javascript arrays object multidimensional-array


【解决方案1】:

使用reduce & filter & map

更新:我添加了一个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)

【讨论】:

  • 感谢您的回答,请您查看问题更新
  • 设备对象是否会包含idtemplateunknown-user-field
  • 是的,完全正确。但所有未知用户字段都必须具有“名称”和“权限”键值对。
  • 更新了答案
【解决方案2】:

你可以使用 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)

【讨论】:

  • 感谢您的回答,请您查看问题更新
猜你喜欢
  • 1970-01-01
  • 2019-04-11
  • 2012-12-07
  • 2013-02-05
  • 1970-01-01
  • 1970-01-01
  • 2012-05-03
相关资源
最近更新 更多