Ali
Ali

Reputation: 10453

Find matching property name in an object and return its value

I have an unknown strucutred JSON object which I want to read all of its property and return its value when the condition is match.

{
      "tags": ["a", "b", "c"],
      "image": "path/to/thumbnail.png",
      "description": "First level description",
      "name": "First name",
      "another": {
        "abcde": {
          "label": "one two three",
          "description": "Oopsy!"
        },
        "fghijk": {
          "label": "Label ABC :)",
          "description": "Is not a song here bruh!"
        }
      },
      "couldBeEmpty": {},
      "couldBeLast": {
        "someCoolName": {
          "label": "Some label here you know",
          "description": "Another Desc Again",
          "type": "text"
        },
        "someLabelAgain": {
          "label": "Just a label name",
          "description": "Yep another Desc :)",
          "type": "color"
        },
        "someObjLabel": {
          "label": "Another label name",
          "description": "Desc label here too",
          "type": "text"
        },
        "ahCoolThing": {
          "label": "Some label for this too",
          "description": "Desc here is fine too",
          "type": "color"
        },
        "couldBeLastInHere": {
          "label": "label",
          "description": "Desc goes here",
          "type": "color"
        }
      }
    }

I want to get all name, label, and description for any given structure of the object then make a new object to be something like this

{
  "description": "First level description",
  "name": "First name",
  "abcde/label": "one two three",
  "abcde/description": "Oopsy!"
  "fghijk/label": "Label ABC :)",
  "fghijk/description": "Is not a song here bruh!"
  "someCoolName/label": "Some label here you know",
  "someCoolName/description": "Another Desc Again",
  "someLabelAgain/label": "Just a label name",
  "someLabelAgain/description": "Yep another Desc :)",
  "someObjLabel/label": "Another label name",
  "someObjLabel/description": "Desc label here too",
  "ahCoolThing/label": "Some label for this too",
  "ahCoolThing/description": "Desc here is fine too",
  "couldBeLastInHere/label": "label",
  "couldBeLastInHere/description": "Desc goes here"
}

Right now I'm trying to use Lodash for this, but couldn't find a way to find all the property with the given name except having to loop through everything

Upvotes: 0

Views: 845

Answers (1)

Ethan Brown
Ethan Brown

Reputation: 27282

What you're looking for is essentially a custom reduction function (you're trying to reduce an object to some compacted representation of its data). Here's an implementation that allows you to specify which fields you want to pluck out:

function myReduce(o, fields, p, c) {
    // p: path
    // c: context (accumulator)
    if(p===undefined) p = '', c = {};
    for(var prop in o) {
        if(!o.hasOwnProperty(prop)) continue;
        if(fields.indexOf(prop)!=-1) c[p + prop] = o[prop];
        else if(typeof o[prop]==='object') myReduce(o[prop], fields, prop + '/', c);
    }
    return c;
}

To get the fields you're interested in, you would call it like this:

myReduce(data, ['name', 'label', 'description']);

Upvotes: 2

Related Questions