// source CSV
var first=JSON.parse('[{"a":"one","b":"123","c":"567"},{"a":"two","b":"2"},{"a":"six","b":"2"},{"a":"three","b":"3"}]');
// source API
var second=JSON.parse('[{"a":"one","b":"1"},{"a":"ten","b":"2"},{"a":"three","b":"3"}]');
// records which deos not exist in API will put in newContent
var newContent = [];
// a temporary row to transfer an unexisting antry to newContent
var row = [];
// default assumtion is that all records from CSV exist in API
var exists=0;
// the outcome array. A merged data from CSV and API
var alltogether = [];
// iterate through CSV
for (i in first) {
// so far the line has been not found
//
// by default let's assume the line is there
exists=0;
// go through other array
for (j in second) {
// compare value
if (first[i].a === second[j].a) {
exists=1;
row = second[j];
Zabbix.Log(4,"existing="+JSON.stringify(row));
// if value exists then imediatelly stop the itereation to save some performance
break;
}
}
// if the previous loop failed to enable flag 'exists=1', it's crear indication the value does not exist in API
if (exists===0) {
Zabbix.Log(4,'"'+first[i].a+'" does not exist in second array');
// here we need to hardcode every line
newContent.push({"a":first[i].a,"b":"new"});
}
}
// merge the existing API array together with the content which does not exist in API
for (el in second) alltogether.push(second[el]);
for (el in newContent) alltogether.push(newContent[el]);
return JSON.stringify({
'first': first,
'second': second,
'alltogether': alltogether
});
No comments:
Post a Comment