0

I have nested object like

{
    "root": {
        "company": [{
                "id": "Google",
                "contact": [{
                        "field": "name",
                        "value": "Larry Page"
                    }, {
                        "field": "flag",
                        "value": ""
                    }, {
                        "field": "initial",
                        "value": "LP"
                    }
                ]
            }, {
                "id": "Snap",
                "contact": [{
                        "field": "name",
                        "value": "Evan Spiegel"
                    }, {
                        "field": "flag",
                        "value": "true"
                    }, {
                        "field": "initial",
                        "value": "ES"
                    }
                ]
            }, {
                "id": "Airbnb",
                "contact": [{
                        "field": "name",
                        "value": "Brian Chesky"
                    }, {
                        "field": "flag",
                        "value": ""
                    }, {
                        "field": "initial",
                        "value": "BC"
                    }
                ]
            }
        ]
    }
}

And i need to create 2 separate lists based on the flag values - one for flag = "" and another for flag = true.

Expected results are the 2 lists:

var flagisTrue = {ES}
var flagisEmpty = [{"name":"Larry Page", "initial": "LP"}, {"name":"Brian Chesky", "initial": "BC"}]

where flagisTrue only contains the initial while flagisEmpty contains the name as well as the initial.

How do I work this structure in JavaScript?

goto
  • 7,908
  • 10
  • 48
  • 58
snorlax
  • 73
  • 8

2 Answers2

1

Try this one. Hope it will help.

var object = {
    "root" : {
        "company" : [{
                "id" : "Google",
                "contact" : [{
                        "field" : "name",
                        "value" : "Larry Page"
                    }, {
                        "field" : "flag",
                        "value" : ""
                    }, {
                        "field" : "initial",
                        "value" : "LP"
                    }
                ]
            }, {
                "id" : "Snap",
                "contact" : [{
                        "field" : "name",
                        "value" : "Evan Spiegel"
                    }, {
                        "field" : "flag",
                        "value" : "true"
                    }, {
                        "field" : "initial",
                        "value" : "ES"
                    }
                ]
            }, {
                "id" : "Airbnb",
                "contact" : [{
                        "field" : "name",
                        "value" : "Brian Chesky"
                    }, {
                        "field" : "flag",
                        "value" : ""
                    }, {
                        "field" : "initial",
                        "value" : "BC"
                    }
                ]
            }
        ]
    }
}

var flagisTrue = [], flagisEmpty = [];
for (var i = 0; i < object.root.company.length; i++) {
    var contact = object.root.company[i].contact;
    var result = {};
    for (var j = 0; j < contact.length; j++)
        result[contact[j].field] = contact[j].value;

    if (result["flag"])
        flagisTrue.push({
            name : result["name"],
            initial : result["initial"]
        });
    else
        flagisEmpty.push({
            name : result["name"],
            initial : result["initial"]
        });
}

console.log(flagisEmpty);
console.log(flagisTrue);
0
var flagIsTrue = jsonObj.root.company.filter(function(c){
  return c.contact.filter(function(_contact){
    return (_contact.field == "flag" && _contact.value == "true");
  }).length > 0;
}).map(function(c){
  return c.contact.filter(function(_contact){
    return _contact.field == "initial";
  }).map(function(_contact){
    return _contact.value
  }).join("");
});

var flagIsEmpty = jsonObj.root.company.filter(function(c){
  return c.contact.filter(function(_contact){
    return (_contact.field == "flag" && !_contact.value);
  }).length > 0;
}).map(function(c){
  var obj = {};
  for(var i = 0, len = c.contact.length ; i < len ; i++) {
    if(c.contact[i].field != "flag")
      obj[c.contact[i].field] = c.contact[i].value;
  }
  return obj;
});
Yonghoon Lee
  • 2,217
  • 1
  • 12
  • 8