hughsk / flat

:steam_locomotive: Flatten/unflatten nested Javascript objects

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Array unflatten isn't correct

vcxiaohan opened this issue · comments

var flat = require('flat')

var obj = [{a: 1}, {b: 2}]
var _obj = flat.flatten(obj)
console.log(_obj)// { '0.a': 1, '1.b': 2 }
console.log(flat.unflatten(_obj))// { '0': { a: 1 }, '1': { b: 2 } }

I want [{a: 1}, {b: 2}], but not { '0': { a: 1 }, '1': { b: 2 } }

A simple possible workaround is that you could wrap the obj array in an additional array so you get:

  const obj = [[{ a: 1 }, { b: 2 }]];
  const flattened = flat(obj);
  console.log(JSON.stringify(flattened)); // {"0.0.a":1,"0.1.b":2}
  const unFlattened = flat.unflatten(flattened);
  console.log(JSON.stringify(unFlattened)); //{"0":[{"a":1},{"b":2}]}