Javascript通过其他值

时间:2017-09-02 12:11:44

标签: 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',
        '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元素的所有“值”条目。

例如

我想基于他们的'权限'属性过滤所有'用户名'(无论在哪个设备中以及用户ID是什么),如果我想要过滤,请获取'John Doe','Bill Doe','Jack Doe','Marry Doe'(作为数组)对于'author1''权限',我可以在任何设备上获得哪些用户拥有'author1'权限。

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

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

现在谢谢。

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

因此,所有设备对象都将具有id,template,unknown-user-field,但所有未知用户字段必须具有'name'和'authority'键值对。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

使用reduce& filter& map

已更新:我添加了一个isLikeUserObj函数,该函数可用于name& authority字段。

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)