如何将对象数组转换为带有键的对象?

时间:2018-09-07 19:51:28

标签: neo4j cypher

我正在尝试将对象数组转换为具有一致键的对象。

样本数据

WITH [ { _type: 'name',
    `display-id`: '0',
    `display-name`: 'Wendall Williams',
    `first-name`: 'Wendall',
    `last-name`: 'Williams' },
  { _type: 'player-number', number: '82' },
  { _type: 'height', inches: '70' },
  { _type: 'weight', pounds: '175' },
  { _type: 'birth-date', date: '18', month: '9', year: '1990' },
  { _type: 'birth-city', city: '' },
  { _type: 'birth-state', abbrev: '', id: '', state: '' },
  { _type: 'birth-country', country: '', abbrev: '', id: '' },
  { _type: 'hometown-city', city: 'Syracuse' },
  { _type: 'hometown-state',
    abbrev: 'NY',
    id: '32',
    state: 'New York' }] AS playerInfo
WITH playerInfo
RETURN playerInfo

我希望使用键_type作为我要创建的对象中属性的键。

我想要的输出如下

{ name: 
   { 'display-id': '0',
     'display-name': 'Wendall Williams',
     'first-name': 'Wendall',
     'last-name': 'Williams' },
  'player-number': { number: '82' },
  height: { inches: '70' },
  weight: { pounds: '175' },
  'birth-date': { date: '18', month: '9', year: '1990' },
  'birth-city': { city: '' },
  'birth-state': { abbrev: '', id: '', state: '' },
  'birth-country': { country: '', abbrev: '', id: '' },
  'hometown-city': { city: 'Syracuse' },
  'hometown-state': { abbrev: 'NY', id: '32', state: 'New York' } }

是否有使用Neo4j构造方法的首选方法?

1 个答案:

答案 0 :(得分:1)

此查询使用几个APOC函数来获取所需的结果:

WITH [ { _type: 'name',
    `display-id`: '0',
    `display-name`: 'Wendall Williams',
    `first-name`: 'Wendall',
    `last-name`: 'Williams' },
  { _type: 'player-number', number: '82' },
  { _type: 'height', inches: '70' },
  { _type: 'weight', pounds: '175' },
  { _type: 'birth-date', date: '18', month: '9', year: '1990' },
  { _type: 'birth-city', city: '' },
  { _type: 'birth-state', abbrev: '', id: '', state: '' },
  { _type: 'birth-country', country: '', abbrev: '', id: '' },
  { _type: 'hometown-city', city: 'Syracuse' },
  { _type: 'hometown-state',
    abbrev: 'NY',
    id: '32',
    state: 'New York' }] AS playerInfo
WITH [x IN playerInfo | [x._type, apoc.map.removeKey(x, '_type')]] AS pairs
RETURN apoc.map.fromPairs(pairs) AS result;