I have a array of events and i want to make relation between these array's object. array:
[{
'area': 'punjab',
'site': 'lahore',
'link': 'http: //lahore.com'
}, {
'area': 'punjab',
'site': 'sargodha',
'link': 'http: //sargodha.com'
}, {
'area': 'punjab',
'site': 'multan',
'link': 'http: //multan.com'
} {
'area': 'sindh',
'site': 'karachi',
'link': 'http: //karachi.com'
}]
Know i want to make the relation like
{
"area": "punjab",
"site": [
{
"name": "lahore",
"link": [
{
"name": "http://sargodha.com",
}
]
},
{
"name": "sargodha",
"link": [
{
"name": "http://sargodha.com",
}
]
}
]
},
{
"area": "sindh",
"site": [
{
"name": "karachi",
"link": [
{
"name": "http://karachi.com",
}
]
}
}
here is my code which i wrote:
function isinarrayfilters(matchingObject, targetArray, key) {
var existindex = -1;
$.each(targetArray, function(index, array) {
if (array.key == matchingObject.key) {
//Object exist in array
existindex = index;
}
});
return existindex;
}
generatedRelationArray = [];
$.each(alertsJson, function(index, alertJson) {
inarrayindex = isinarrayfilters(alertJson.area, relationArray,'area');
if (inarrayindex == -1) {
key = alertJson.site
generatedsites = {
"area": alertJson.area,
"site": []
}
relationArray.push(generatedsites);
}
});
Know anyone guide me how i append the site into the related area.
- I have to run loop again and try to check the exist method and get the index and will push the sites into it?