简体   繁体   English

从基于两个键的对象的两个数组中查找唯一值,即在JavaScript中从and到

[英]finding unique values from two arrays of objects based on two keys i.e from and to in javascript

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. 我想要的是两个以上对象的数组,它们具有唯一的“ from”,“ to”和“ style”应该是“”(空白)或undefined.with唯一的ID。

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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据两个键从对象数组中获取唯一值 - Get unique values from an array of objects based on two keys 如何从 JavaScript 中的两个二维数组中获取唯一值 - How to get unique values from two 2D arrays in JavaScript 如何根据javascript中的键合并和替换两个数组中的对象? - How to merge and replace objects from two arrays based on a key in javascript? 使用JavaScript使用两个数组中的不同值创建对象数组 - Create array of objects using distinct values from two arrays with javascript 在两个 JavaScript 对象数组中查找值之间的差异 - Finding Difference Between Values in Two JavaScript Arrays of Objects 从 typescript 中的对象列表中查找键的唯一值的高效方法 - Performant way of finding unique values of keys from a list of objects in typescript 基于两个或多个键的对象数组和动态收集的值数组对对象进行分组 - Group objects from an array of objects based on two or more keys and from an array of dynamically collected values 从JavaScript中的两个数组生成对象数组 - Generate array of objects from two arrays in JavaScript 如何使用最新的 Javascript 合并两个对象,使一个对象的键映射到另一个对象的值,而第二个对象没有额外的键? - How do I merge two objects such that keys from one map to values of another, without the extra keys in the second, using latest Javascript? 通过值为数组的键组合两个对象 - Combining two objects by keys whose values are arrays
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM