i have two arrays of objects like shown below :
var b = [{"from":2,"to":7,"id":1},{"from":3,"to":9,"id":2},{"from":2,"to":7,"id":3}]
var c = [{"from":3,"to":9,"id":2,"style":""},{"from":2,"to":7,"id":3,"style":"dash-line"},{"from":4,"to":2,"id":4,"style":"dash-line"},{"from":2,"to":4,"id":5,"style":""},{"from":4,"to":2,"id":6,"style":"dash-line"}];
what i want is an array of objects from above two , which has unique "from" ,"to" and "style" should be either ""(blank) or undefined.With unique ids.
ie
output = [{"from":2,"to":7,"id":0},{"from":3,"to":9,"id":1},{"from":2,"to":4,"id":6,"style":""}]
am able to get it as shown in below code, but i feel code can be optimized or there can be a better way to do it. Please help....Thanks.
var b = [{"from":2,"to":7,"id":1},{"from":3,"to":9,"id":2},{"from":2,"to":7,"id":3}]
var c = [{"from":3,"to":9,"id":2,"style":""},{"from":2,"to":7,"id":3,"style":"dash-line"},{"from":4,"to":2,"id":4,"style":"dash-line"},{"from":2,"to":4,"id":5,"style":""},{"from":4,"to":2,"id":6,"style":"dash-line"}];
var a = b.concat(c);
findUniQue(a);
function findUniQue(a){
var tempArr =[];
for(var i =0;i<a.length;i++){
if(a[i].style == undefined || a[i].style != 'dash-line' ){
var count = 0;
if(tempArr.length>0){
for(var j =0;j<tempArr.length;j++){
if((a[i].from == tempArr[j].from)&&(a[i].to == tempArr[j].to)){
count--;
break;
}
else{
count++;
}
if(count == tempArr.length){
a[i].id = i;
tempArr.push(a[i]);
}
}
}
else{
a[i].id = i;
tempArr.push(a[i]);
}
}
}
console.dir(tempArr);
}
function removeduplicate(){
var array = [{id:5},{id:8},{id:9},{id:10},{id:5},{id:8}];
var size = array.length;
for (var i = 0; i < size - 1; i++) {
for (var j = i + 1; j < size; j++) {
if (array[j].id !== array[i].id)
continue;
array.splice(j,1);
j--;
size--;
} // for j
} // for i
console.log(array);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.