如何在JavaScript中从2个对象获取常用项?

时间:2015-05-27 04:34:16

标签: javascript arrays algorithm

我有两个像这样的对象数组:

数组1

[{"id":1,"relationship_type_id":"3"},
{"id":2,"relationship_type_id":"2"},
{"id":3,"relationship_type_id":"1"},]

数组2

[{"relationship_type_id":1,"contributing":"yes"},
{"relationship_type_id":2,"contributing":"no"},
{"relationship_type_id":3,"contributing":"yes"}]

如何找出阵列1中哪个ids具有贡献的关系类型,即贡献=="是"基于数组2?

结果应如下所示:

结果1

[{"id":1}, {"id":3}]

或者更进一步,只返回贡献关系的数量

结果2

contributingRelationship = 2

5 个答案:

答案 0 :(得分:3)

您可以将.filter().some().map()结合使用。

如果arr2特别大,您将首先计算贡献类型并将其保留在地图中,以便在过滤arr1期间更快地检索。



var arr1 = [{"id":1,"relationship_type_id":"3"},
{"id":2,"relationship_type_id":"2"},
{"id":3,"relationship_type_id":"1"}];

var arr2 = [{"relationship_type_id":1,"contributing":"yes"},
{"relationship_type_id":2,"contributing":"no"},
{"relationship_type_id":3,"contributing":"yes"}];

var contributing = arr1.filter(function(item) {
  // filter based on whether arr2 has corresponding item
  return arr2.some(function(other_item) {
    return other_item.relationship_type_id == item.relationship_type_id && other_item.contributing == "yes";
  });
}).map(function(item) {
  // map into final representation
  return {
    id: item.id
  };
});

console.log(contributing);




答案 1 :(得分:2)

这样的事情怎么样:

(function(){
    var contributingIds = [];
    var result = [];

    var arr = [
        {"id":1,"relationship_type_id":"3"},
        {"id":2,"relationship_type_id":"2"},
        {"id":3,"relationship_type_id":"1"}
    ];

    var arr2 = [
        {"relationship_type_id":1,"contributing":"yes"},
        {"relationship_type_id":2,"contributing":"no"},
        {"relationship_type_id":3,"contributing":"yes"}
    ];

    arr2.forEach(function(item){
        if(item.contributing === 'yes'){
            contributingIds.push(item.relationship_type_id);
        }
    });

    arr.forEach(function(item){
        if(contributingIds.indexOf(parseInt(item.relationship_type_id, 10)) >= 0){
            result.push(item.id);
        }
    });

    console.log(result);
})();

答案 2 :(得分:0)

你可以这样做:

var idarray = [{"id":1,"relationship_type_id":"3"},
{"id":2,"relationship_type_id":"2"},
{"id":3,"relationship_type_id":"1"},]; 


var relarray = [{"relationship_type_id":1,"contributing":"yes"},
{"relationship_type_id":2,"contributing":"no"},
{"relationship_type_id":3,"contributing":"yes"}];

var resultArray = [];

for (var counter=0; counter < idarray.length; counter++) {
        for (var relarrayCounter =0; relarrayCounter < relarray.length; relarrayCounter++) {
            if(idarray[counter].relationship_type_id== relarray[relarrayCounter].relationship_type_id) {
               if (relarray[relarrayCounter].contributing == "yes") {
                   var resultObj = {};
                   resultObj.id = idarray[counter].relationship_type_id;
                   resultArray.push(resultObj);
               }
            }

        }
    }
console.log("Result =", resultArray);
console.log("Contributing relationship = ", resultArray.length);

答案 3 :(得分:0)

我使用了两个简单的foreach循环来遍历每组哈希,寻找指定的输入id。我希望你觉得它很有用。

&#13;
&#13;
    // initialize the arrays
    var id_to_relation = [{"id":1,"relationship_type_id":"3"},
    {"id":2,"relationship_type_id":"2"},
    {"id":3,"relationship_type_id":"1"}]
    var relation_to_contribute = [{"relationship_type_id":1,"contributing":"yes"},
    {"relationship_type_id":2,"contributing":"no"},
    {"relationship_type_id":3,"contributing":"yes"}]
    
    window.getRelation = function(id)
    {
        var type = [];  // initialize relationship_type_id holder
        var contributing = 0;  // current count of contributing ids
        for (var hash in id_to_relation)
        {
            if (id_to_relation[hash]["id"] == id)
            {
                type.push(parseInt(id_to_relation[hash]["relationship_type_id"]));  // grab the related types
            }
        }
        for (var hash in relation_to_contribute)
        { 
            if (type.indexOf(relation_to_contribute[hash]["relationship_type_id"]) > -1)
            {
                contributing += relation_to_contribute[hash]["contributing"] == "yes" ? 1 : 0;
            }
        }
        document.getElementById("result").innerHTML = "contributingRelationship = " + contributing;
    }
&#13;
Enter requested id: <input type="number" id="thisId" />
<br>
<button type="button" onclick="getRelation(document.getElementById('thisId').value)">
  Check id
</button>
<h1>Result</h1>
<p id="result"></p>
&#13;
&#13;
&#13;

答案 4 :(得分:0)

这也应该有用。

{{1}}

dbf