3

我有两个对象数组:

var arOne = [
    {key: 'alpha', value: 5},  
    {key: 'beta', value: 11},  
    {key: 'gamma', value: 15},  
    {key: 'delta', value: 12},  
    {key: 'epsilon', value: 55}  
    {key: 'pony', value: 101}
]

var arTwo = [
    {key: 'alpha', value: 5.5},  
    {key: 'beta', value: 11.5},  
    {key: 'gamma', value: 15.5},  
    {key: 'psi', value: 12.5},  
    {key: 'omega', value: 55.5}  
]

我需要将值合并到一个数组中。

键匹配的情况:使用键创建一个数组并将值附加arTwoarOne

键不匹配的情况:如果键存在于 中arOne,我将值 fromarOne和 0包括在内arTwo。如果密钥存在于 中arTwo,我将包含一个 0arOne和来自 的值arTwo

请注意arOnearTwo可能有不同的大小(请参阅 中的ponyarOne)。

结果应该是这样的:

var result = [
    ['alpha', 5, 5.5],  
    ['beta', 11, 11.5],  
    ['gamma', 15, 15.5],  
    ['delta', 12, 0],  
    ['epsilon', 55, 0],  
    ['pony', 101, 0],
    ['psi', 0, 12.5],  
    ['omega', 0, 55.5],  
]

我一整天都在盯着这个,并抓了我所有的尝试。有什么想法吗?

4

3 回答 3

2
function combine(obj1, obj2){
    var arr = [], k, i;
    for (k = 0; k < obj1.length; ++k){
        arr.push([obj1[k].key, obj1[k].value]);
    }
    for (k = 0; k < obj2.length; ++k){
        var exists = false;
        for (i = 0; i < arr.length; ++i){
            if(arr[i][0] === obj2[k].key){
               arr[i].push(obj2[k].value);
               var exists = true; 
               break;
            }
        }
        if (!exists){
            arr.push([obj2[k].key, 0, obj2[k].value]);
        }
    }
    for (i = 0; i < arr.length; ++i){
        if (arr[i].length === 2){
            arr[i].push(0);
        }
    }

    return arr;
}

像这样调用:

var myNewArray = combine(arOne, arTwo);
于 2012-12-07T19:59:36.647 回答
1

这里简单介绍一下。您遍历第一个数组并将数据推送到结果数组中。当你循环第二个时有点棘手。您获取每个键值对并检查该键是否已存在于结果数组中。如果是,则添加它,如果不是( found = false )将其添加到结果数组中:

var result = [];
var i;

for ( i = 0; i < arOne.length; i++ ) {
    result.push( [ arOne[ i ].key, arOne[ i ].value ] );
}

for ( i = 0; i < arTwo.length; i++ ) {
    var key = arTwo[ i ].key;
    var value = arTwo[ i ].value;
    var found = false;

    for ( var k = 0; k < result.length; k++ ) {
        if ( result[ k ][ 0 ] === key ) {
            result[ k ].push( value );
            found = true;
        }
    }

    if ( ! found ) {
        result.push( [ key, value ] );
    }

}

结果是您想要的数组。可能不是最有效的方法。但它可以完成工作。

另外,在一个小笔记上。您可能需要考虑使用 and object 而不是嵌套数组。所以你的结果看起来像这样:

var result = {
    alpha: [5, 5.5],  
    beta: [11, 11.5],  
    gamma: [15, 15.5],  
    delta: [12, 0],  
    epsilon: [55, 0],  
    pony: [101, 0],
    psi: [0, 12.5],  
    omega: [0, 55.5], 
}

这可能更容易在代码中稍后处理您的数据。

于 2012-12-07T20:01:26.450 回答
1

好的..我做到了

var tmp_result = {}; // we need this object to make things easy
var result = [];

// add all elements in first array to result, im using this as an object so that I can easily find the keys later
$.each(arOne, function (index, element) {
    tmp_result[element.key] = [element.key, element.value];
});


// loop through second array    
$.each(arTwo, function (index, element) {
    // check if this was key already added
    if (tmp_result[element.key] != undefined) {
        // if it was added earlier then add the new key
        tmp_result[element.key].push(element.value);
    } else {
        // create a new key, add 0 in between since its not there in first array
        tmp_result[element.key] = [element.key, 0, element.value];
    }
});

// now we are left with all those elements which are in first array but not in second
$.each(tmp_result, function (key, element) {
    // check if the array has 3 elements, if not then add 0 in the end since its not there in second one
    if (element.length < 3) element.push(0);
    // pass the flattened out data to result array
    result.push(element);
});

console.log(result);

小提琴 - http://jsfiddle.net/atif089/McfUk/

我的输出是

[
    ["alpha", 5, 5.5], 
    ["beta", 11, 11.5], 
    ["gamma", 15, 15.5], 
    ["delta", 12, 0], 
    ["epsilon", 55, 0], 
    ["pony", 101, 0], 
    ["psi", 0, 12.5], 
    ["omega", 0, 55.5]
]
于 2012-12-07T20:27:56.410 回答