我有两个对象数组:
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}
]
我需要将值合并到一个数组数组中。
密钥匹配的情况:使用密钥创建一个数组,并将arTwo
的值附加到arOne
。
密钥不匹配的情况:如果密钥存在于arOne
中,我会包含arOne
的值和arTwo
的0。如果密钥存在于arTwo
中,则我为arOne
添加0,并为arTwo
添加值。
请注意,arOne
和arTwo
的大小可能不同(请参阅pony
中的arOne
键)。
这就是结果应该是这样的:
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],
]
我整天都在盯着这一切并且抓住了我的所有尝试。有什么想法吗?
答案 0 :(得分: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);
答案 1 :(得分:1)
这里很简单。循环遍历第一个数组并将数据推送到结果数组中。当你循环第二个时,有点棘手。您获取每个键值对并检查该键是否已存在于结果数组中。如果是这样添加它,如果没有(找到= 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 ] );
}
}
结果是你想要的数组。可能不是最有效的方法。但它完成了工作。
另外,小小的说明。您可能需要考虑使用和对象而不是嵌套数组。 所以你的结果看起来像这样:
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],
}
稍后在代码中处理数据可能会更容易。
答案 2 :(得分: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]
]