user3686794 user3686794 - 2 months ago 9
AngularJS Question

push and replacing the json array in an existing array in angularjs

my json data:

{
"items": [
{
"propertyId": 9,
"hp_builders_id": 1,
"property_name": "Godrej Avenues",
"properties": [
{
"UnitName": "1 BHK+1T",
"property_size": "629 ",
"bedrooms": 1,
"unit_type_name": "1 BHK",
"unit_type_status": 1,
"UnitPrice": 2704071,
"price_per_sqft": "4299",
"hp_property_id": 9,
"unit_price_id": 45
},
{
"UnitName": "1 BHK+1T",
"property_size": "706 ",
"bedrooms": 1,
"unit_type_name": "1 BHK",
"unit_type_status": 1,
"UnitPrice": 3035094,
"price_per_sqft": "4299",
"hp_property_id": 9,
"unit_price_id": 46
},
{
"UnitName": "2 BHK+2T",
"property_size": "1042 ",
"bedrooms": 2,
"unit_type_name": "2 BHK",
"unit_type_status": 1,
"UnitPrice": 4479558,
"price_per_sqft": "4299",
"hp_property_id": 9,
"unit_price_id": 47
},
{
"UnitName": "2 BHK+2T",
"property_size": "1045 ",
"bedrooms": 2,
"unit_type_name": "2 BHK",
"unit_type_status": 1,
"UnitPrice": 4492455,
"price_per_sqft": "4299",
"hp_property_id": 9,
"unit_price_id": 48
},
{
"UnitName": "2 BHK+2T",
"property_size": "1050 ",
"bedrooms": 2,
"unit_type_name": "2 BHK",
"unit_type_status": 1,
"UnitPrice": 4513950,
"price_per_sqft": "4299",
"hp_property_id": 9,
"unit_price_id": 49
},
{
"UnitName": "2 BHK+2T",
"property_size": "1089 ",
"bedrooms": 2,
"unit_type_name": "2 BHK",
"unit_type_status": 1,
"UnitPrice": 4681611,
"price_per_sqft": "4299",
"hp_property_id": 9,
"unit_price_id": 50
},
{
"UnitName": "3 BHK+3T",
"property_size": "1521 ",
"bedrooms": 3,
"unit_type_name": "3 BHK",
"unit_type_status": 1,
"UnitPrice": 6538779,
"price_per_sqft": "4299",
"hp_property_id": 9,
"unit_price_id": 51
},
{
"UnitName": "3 BHK+3T",
"property_size": "1523 ",
"bedrooms": 3,
"unit_type_name": "3 BHK",
"unit_type_status": 1,
"UnitPrice": 6547377,
"price_per_sqft": "4299",
"hp_property_id": 9,
"unit_price_id": 52
},
{
"UnitName": "3 BHK+3T",
"property_size": "1525 ",
"bedrooms": 3,
"unit_type_name": "3 BHK",
"unit_type_status": 1,
"UnitPrice": 6555975,
"price_per_sqft": "4299",
"hp_property_id": 9,
"unit_price_id": 53
},
{
"UnitName": "3 BHK+3T",
"property_size": "1526 ",
"bedrooms": 3,
"unit_type_name": "3 BHK",
"unit_type_status": 1,
"UnitPrice": 6560274,
"price_per_sqft": "4299",
"hp_property_id": 9,
"unit_price_id": 54
}
]
},
{
"builders": {
"builders_id": 1,
"builders_name": "Godrej",
"builders_status": 1,
"created_date": "2016-07-27T00:00:00.000Z",
"update_date": "2016-08-17T09:41:21.000Z"
},
"propertyId": 1,
"hp_builders_id": 1,
"property_name": "Godrej Woodsman Estate",
"property_latitude": "13.0544261",
"property_longitude": "77.5918467",
"property_status": 1,
"created_date": "2016-08-01T10:17:34.000Z",
"update_date": "2016-09-07T03:39:45.000Z",
"properties": [
{
"UnitName": "2 BHK+2T",
"property_size": "1400",
"bedrooms": 2,
"unit_type_name": "2 BHK",
"unit_type_status": 1,
"UnitPrice": 11400200,
"price_per_sqft": "8143",
"hp_property_id": 1,
"unit_price_id": 81
},
{
"UnitName": "3 BHK+3T",
"property_size": "1835",
"bedrooms": 3,
"unit_type_name": "3 BHK",
"unit_type_status": 1,
"UnitPrice": 16999440,
"price_per_sqft": "9264",
"hp_property_id": 1,
"unit_price_id": 84
},
{
"UnitName": "3 BHK+3T",
"property_size": "1729",
"bedrooms": 3,
"unit_type_name": "3 BHK",
"unit_type_status": 1,
"UnitPrice": 16000166,
"price_per_sqft": "9254",
"hp_property_id": 1,
"unit_price_id": 85
},
{
"UnitName": "2 BHK+2T",
"property_size": "1460",
"bedrooms": 2,
"unit_type_name": "2 BHK",
"unit_type_status": 1,
"UnitPrice": 18501120,
"price_per_sqft": "12672",
"hp_property_id": 1,
"unit_price_id": 86
},
{
"UnitName": "2 BHK+2T",
"property_size": "1325",
"bedrooms": 2,
"unit_type_name": "2 BHK",
"unit_type_status": 1,
"UnitPrice": 13501750,
"price_per_sqft": "10190",
"hp_property_id": 1,
"unit_price_id": 87
},
{
"UnitName": "2 BHK+2T",
"property_size": "1460",
"bedrooms": 2,
"unit_type_name": "2 BHK",
"unit_type_status": 1,
"UnitPrice": 16000140,
"price_per_sqft": "10959",
"hp_property_id": 1,
"unit_price_id": 88
},
{
"UnitName": "3 BHK+3T",
"property_size": "1800",
"bedrooms": 3,
"unit_type_name": "3 BHK",
"unit_type_status": 1,
"UnitPrice": 21000600,
"price_per_sqft": "11667",
"hp_property_id": 1,
"unit_price_id": 89
},
{
"UnitName": "3 BHK+3T",
"property_size": "1750",
"bedrooms": 3,
"unit_type_name": "3 BHK",
"unit_type_status": 1,
"UnitPrice": 15001000,
"price_per_sqft": "8572",
"hp_property_id": 1,
"unit_price_id": 90
},
{
"UnitName": "4 BHK+4T",
"property_size": "2302",
"bedrooms": 4,
"unit_type_name": "4 BHK",
"unit_type_status": 1,
"UnitPrice": 29999664,
"price_per_sqft": "13032",
"hp_property_id": 1,
"unit_price_id": 91
},
{
"UnitName": "2 BHK+2T",
"property_size": "1459",
"bedrooms": 2,
"unit_type_name": "2 BHK",
"unit_type_status": 1,
"UnitPrice": 13999105,
"price_per_sqft": "9595",
"hp_property_id": 1,
"unit_price_id": 92
}
]
}
]
}


but i want to change the response of that json as like this :

{
"items": [
{
"propertyId": 9,
"hp_builders_id": 1,
"property_name": "Godrej Avenues",
"property_start_date": "0000-00-00",
"property_completion_date": "2019-12-01T00:00:00.000Z",
"property_launch_date": "2019-12-01T00:00:00.000Z",
"total_launched_apartments": 420,
"total_property_area": 5,
"property_description": "Godrej Properties presents Godrej Avenues at Yelahanka, Bengaluru with 1, 2, 2.5 and 3 BHK homes that keep you happy every day. Located on the Yelahanka-Doddaballapur Main Road, the 10 majestic towers spread over 5.3 Acres with a Sky Lounge set at 45.72 metre you experience the high life right at your doorstep. At Godrej Avenues, come home to a world of happiness every day.",
"property_latitude": "13.1495363",
"property_longitude": "77.5610062",
"property_status": 1,
"created_date": "2016-09-06T02:49:09.000Z",
"update_date": "2016-09-06T03:22:56.000Z",
"properties": [

{"UnitName": "1bhk"
data:[
{
"UnitName": "1 BHK+1T",
"property_size": "629 ",
"bedrooms": 1,
"unit_type_name": "1 BHK",
"unit_type_status": 1,
"UnitPrice": 2704071,
"price_per_sqft": "4299",
"hp_property_id": 9,
"unit_price_id": 45
},
{
"UnitName": "1 BHK+1T",
"property_size": "706 ",
"bedrooms": 1,
"unit_type_name": "1 BHK",
"unit_type_status": 1,
"UnitPrice": 3035094,
"price_per_sqft": "4299",
"hp_property_id": 9,
"unit_price_id": 46
}]
},
{"UnitName": "2bhk"
data:[
{
"UnitName": "2 BHK+1T",
"property_size": "629 ",
"bedrooms": 1,
"unit_type_name": "1 BHK",
"unit_type_status": 1,
"UnitPrice": 2704071,
"price_per_sqft": "4299",
"hp_property_id": 9,
"unit_price_id": 45
},
{
"UnitName": "2 BHK+1T",
"property_size": "706 ",
"bedrooms": 1,
"unit_type_name": "1 BHK",
"unit_type_status": 1,
"UnitPrice": 3035094,
"price_per_sqft": "4299",
"hp_property_id": 9,
"unit_price_id": 46
}]
}

]
}
}
]
}


so based on bedrooms,i want to separate that elements like (1bhk as 1bhk+1t, 1bhk+2t and 2bhk as 2bhk+1t, 2bhk+2t).so based on condition how to change the code

my code :

for(var property in clubproperty){
console.log(clubproperty[property]);

if(clubproperty[property].bedrooms=='1'){

console.log(clubproperty[property].UnitName);
items=clubproperty[property];
console.log(items);
a.properties[groupProp].unitname="1 BHK";

properties:[{
unitname:'3bhk',
items:[]

}]

}


}

Answer

You may try this approach:

var source = {"items": [] };

source.items.map(function(item){
    var result =
       item.properties.reduce(function(p, c){           
           p[c.unit_type_name] = p[c.unit_type_name] || [];

           p[c.unit_type_name].push(c);

           return p;
       }, {});

   return Object.keys(result).reduce(function(p, c){
       return { UnitName : c, data: result[c] }
   }, {});
 });