Javascript合并两个具有公共匹配元素的多维数组 [英] Javascript merging two multidimensional arrays with common matching elements

查看:111
本文介绍了Javascript合并两个具有公共匹配元素的多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个多维数组,并希望将其合并为一个仅包含常见匹配标记的数据源。

I have two multi-dimensional arrays and would like to merge this into a single data source that contains only the common matching tags.

// jsfiddle
http://jsfiddle.net/Qh9X5/10173/

//jsfiddle http://jsfiddle.net/Qh9X5/10173/

//数组1

    var array1 = [{
            "Skills & Expertise": [{
              "id": 2,
              "tag": "Javascript"
            }, {
              "id": 3,
              "tag": "Design"
            }],
            "Location": [{
              "id": 0,
              "tag": "London"
            }, {
              "id": 1,
              "tag": "Germany"
            }],
            "Company": [{
              "id": 0,
              "tag": "Cheesestrings"
            }]
}];

// array 2

//array 2

var array2 = [{
            "Skills & Expertise": [{
              "id": 0,
              "tag": "JAVA"
            }, {
              "id": 1,
              "tag": "PHP"
            }, {
              "id": 2,
              "tag": "Javascript"
            }],
            "Location": [{
              "id": 0,
              "tag": "London"
            }],
            "Company": [{
              "id": 0,
              "tag": "Cheesestrings"
            }, {
              "id": 1,
              "tag": "Bakerlight"
            }]
          }]

所以结果应该是这样的

//期望的结果

  var array3 = [{
                "Skills & Expertise": [{
                  "id": 2,
                  "tag": "Javascript"
                }],
                "Location": [{
                  "id": 0,
                  "tag": "London"
                }],
                "Company": [{
                  "id": 0,
                  "tag": "Cheesestrings"
                }]
    }];

我是否可以通过使用联系人合并两个数组来开始 - 然后删除不使用联系人的元素存在于两者?

Would I start out by merging both arrays using a contact -- and then removing elements that don't exist in both?

var array3 = array1.concat(array2); // Merges both arrays


推荐答案

您可以使用哈希表它反映了数组项并使用嵌套方法来获取哈希值和结果集。

You could use a hash table which reflects the array items and use a nested approach for getting the hash and the result set.

var array1 = [{ "Skills & Expertise": [{ id: 2, tag: "Javascript" }, { id: 3, tag: "Design" }], Location: [{ id: 0, tag: "London" }, { id: 1, tag: "Germany" }], Company: [{ id: 0, tag: "Cheesestrings" }] }],
    array2 = [{ "Skills & Expertise": [{ id: 0, tag: "JAVA" }, { id: 1, tag: "PHP" }, { id: 2, tag: "Javascript" }], Location: [{ id: 0, tag: "London" }], Company: [{ id: 0, tag: "Cheesestrings" }, { id: 1, tag: "Bakerlight" }] }],
    hash = [],
    result;

array1.forEach(function (o, i) {
    Object.keys(o).forEach(function (k) {
        o[k].forEach(function (a) {
            hash[i] = hash[i] || {};
            hash[i][[k, a.tag].join('|')] = true;
        });
    });
});

result = array2.map(function (o, i) {
    var temp = {};
    Object.keys(o).forEach(function (k) {
        o[k].forEach(function (a) {
            if ((hash[i] || {})[[k, a.tag].join('|')]) {
                temp[k] = temp[k] || [];
                temp[k].push(a);
            }
        });
    });
    return temp;
});

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }

这篇关于Javascript合并两个具有公共匹配元素的多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆