Как мне преобразовать массив объектов в объект с ключами? - PullRequest
0 голосов
/ 07 сентября 2018

Я пытаюсь преобразовать массив объектов в объект, в котором есть согласованный ключ.

Пример данных

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 голосов
/ 08 сентября 2018

В этом запросе используется пара функций 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;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...