如何使用键将对象数组转换为对象?

问题描述 投票:0回答:1

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

样本数据

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结构的首选方法?

neo4j cypher
1个回答
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;
© www.soinside.com 2019 - 2024. All rights reserved.