本文介绍了按键组合对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照我的案例ID中的键组合/合并2个对象数组。

I am trying to combine/merge 2 array of objects by key in my case id.

目标:


  1. 我期待一个结果,我的数组中包含所有带有id 1,2,3,4的对象,例如

  2. 合并顺序不应影响结果中的对象数量,例如 combine(arr1,arr2) combine(arr2,arr1)应该包含具有相同数量对象的数组

  3. 合并顺序只能影响结果对象,例如在 combine(arr1,arr2)的情况下 arr2键,值对可以覆盖arr1键,值就像深度jquery extend $ .extend(true,arr1ObJ,arr2ObJ);

  1. I am expecting a results where I would have array containing all objects with ids 1,2,3,4 as per example
  2. Order of merging should not affect number of objects in result for example combine(arr1,arr2) or combine(arr2,arr1) should have array with same number of objects
  3. Order of merging can only affect resulting object for example in case of combine(arr1,arr2) arr2 key,values pair can override arr1 key,values just like deep jquery extend $.extend( true, arr1ObJ,arr2ObJ );

JSFIDDLE:

JSFIDDLE: https://jsfiddle.net/bababalcksheep/u2c05nyj/

示例数据:

var arr1 = [{
  id: 1,
  name: "fred",
  title: "boss"
}, {
  id: 2,
  name: "jim",
  title: "nobody"
}, {
  id: 3,
  name: "bob",
  title: "dancer"
}];
var arr2 = [{
  id: 1,
  wage: "300",
  rate: "day"
}, {
  id: 2,
  wage: "10",
  rate: "hour"
}, {
  id: 4,
  wage: "500",
  rate: "week"
}];
var Result = [{
  "id": 1,
  "name": "fred",
  "title": "boss",
  "wage": "300",
  "rate": "day"
}, {
  "id": 2,
  "name": "jim",
  "title": "nobody",
  "wage": "10",
  "rate": "hour"
}, {
  id: 3,
  name: "bob",
  title: "dancer" 
}, {
  id: 4,
  wage: "500",
  rate: "week"
}];


推荐答案

如何做到这一点:

function combineArrays(arr1, arr2, keyFunc) {
    var combined = [],
        keys1 = arr1.map(keyFunc),
        keys2 = arr2.map(keyFunc),
        pos1 = keys1.map(function (id) {
            return keys2.indexOf(id);
        }),
        pos2 = keys2.map(function (id) {
            return keys1.indexOf(id);
        });

    arr1.forEach(function (item, i) {
        combined.push( $.extend(item, arr2[pos1[i]]) );
    });
    arr2.forEach(function (item, i) {
        if (pos2[i] === -1) combined.push( item );
    });
    return combined;    
}

用作

var combine = combineArrays(arr1, arr2, function (item) {
    return item.id;
});
var arr1 = [
    { id: 1, name: 'fred', title: 'boss' },
    { id: 2, name: 'jim', title: 'nobody' },
    { id: 3, name: 'bob', title: 'dancer' }
];
var arr2 = [
    { id: 1, wage: '300', rate: 'day' },
    { id: 2, wage: '10', rate: 'hour' },
    { id: 4, wage: '500', rate: 'week' }
];

function combineArrays(arr1, arr2, keyFunc) {
    var combined = [],
        keys1 = arr1.map(keyFunc),
        keys2 = arr2.map(keyFunc),
        pos1 = keys1.map(function (id) {
            return keys2.indexOf(id);
        }),
        pos2 = keys2.map(function (id) {
            return keys1.indexOf(id);
        });

    arr1.forEach(function (item, i) {
        combined.push( $.extend(item, arr2[pos1[i]]) );
    });
    arr2.forEach(function (item, i) {
        if (pos2[i] === -1) combined.push( item );
    });
    return combined;
}

var combine = combineArrays(arr1, arr2, function (item) {
    return item.id;
});

output(combine);



//
//
//
/* pretty Print */
function output(inp) {
    var str = JSON.stringify(inp, undefined, 4);
    $('body').append($('<pre/>').html(str));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

这篇关于按键组合对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 19:51