【问题标题】:Javascript merging two multidimensional arrays with common matching elementsJavascript合并两个具有共同匹配元素的多维数组
【发布时间】:2017-09-04 12:47:31
【问题描述】:

我有两个多维数组,想将其合并到一个只包含常见匹配标签的数据源中。

//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"
            }]
}];

//数组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"
                }]
    }];

我会先使用联系人合并两个数组,然后删除两个数组中都不存在的元素吗?

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

【问题讨论】:

    标签: javascript arrays multidimensional-array merge


    【解决方案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" }] }],
        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; }

    【讨论】:

    猜你喜欢
    • 2012-03-04
    • 2019-12-14
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    • 2016-10-25
    • 1970-01-01
    • 1970-01-01
    • 2013-07-09
    相关资源
    最近更新 更多