Home:ALL Converter>How to enumerate complex javascript object

How to enumerate complex javascript object

Ask Time:2018-03-06T15:22:46         Author:jackjack

Json Formatter

How to enumerate complex javascript object to get property paths to plain values?

In example if object is:

let complex = {
  person: {name: 'mat', age: 31},
  car: {
      model: 'Volsan',
      engine: 'large', 
      doors:[
        { side:'right front', color: 'blue' },
        { side:'left rear', color: 'red' }
      ]
   }
};

and outcome would be like:

complex.person.name
complex.person.age
complex.car.model
complex.car.engine
complex.car.doors[0].side
complex.car.doors[0].color
complex.car.doors[1].side
complex.car.doors[1].color

so that there would be only those values that are "ending the graph"

eg. it would be reverse _.at from lodash: https://lodash.com/docs/4.17.5#at

Author:jackjack,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/49125398/how-to-enumerate-complex-javascript-object
yy