将obj1和obj2等对象合并到javascript中获取obj3。
obj1 = [{fruit: 'watermelon', sweetness: 3},{fruit: 'banana', sweetness: 4},{fruit: 'apple', sweetness: 5}]
obj2 = [{fruit_name: 'apple', color: 'red'},{fruit_name: 'banana', color:'yellow'},{fruit_name: 'watermelon', color:'green'}]
obj3 = [{fruit_name: 'apple', color: 'red', sweetness: 5},{fruit_name: 'banana', color:'yellow', sweetness: 4},{fruit_name: 'watermelon', color:'green', sweetness: 3}]
答案 0 :(得分:1)
不是一般解决方案,但足以满足您的需求:
var obj1 = [{fruit: 'watermelon', sweetness: 3},{fruit: 'banana', sweetness: 4},{fruit: 'apple', sweetness: 5}]
var obj2 = [{fruit_name: 'apple', color: 'red'},{fruit_name: 'banana', color:'yellow'},{fruit_name: 'watermelon', color:'green'}]
var obj3 = obj2.map(o => Object.assign({}, o, {'sweetness': obj1.find(p => p.fruit === o.fruit_name).sweetness}))
console.log(obj3)

答案 1 :(得分:0)
您的数据结构不正确。你无法保存"'水果:'西瓜'" (数组中的键,值对)。
会出错:Uncaught SyntaxError: Unexpected token :
我假设你要做的是:
obj1 = [{fruit: 'watermelon', sweetness: 3},{fruit: 'banana', sweetness: 4},{fruit: 'apple', sweetness: 5}]
obj2 = [{fruit_name: 'apple', color: 'red'},{fruit_name: 'banana', color:'yellow'},{fruit_name: 'watermelon', color:'green'}]
obj3 = [];
for (i = 0; i < obj1.length; i++) {
for (j = 0; j < obj2.length; j++) {
if (obj1[i].fruit === obj2[j].fruit_name) {
var temp = {
fruit_name: obj2[j].fruit_name,
color: obj2[j].color,
sweetness: obj1[i].sweetness
}
obj3.push(temp);
}
}
}
console.log(obj3);
&#13;
答案 2 :(得分:0)
您可以首先通过使用#forEach
合并类似的对象然后提取所需的数组来创建hashtable
使用#map()
函数 - 请参阅下面的演示:
var obj1 = [{fruit: 'watermelon', sweetness: 3},{fruit: 'banana', sweetness: 4},{fruit: 'apple', sweetness: 5}],
obj2 = [{fruit_name: 'apple', color: 'red'},{fruit_name: 'banana', color:'yellow'},{fruit_name: 'watermelon', color:'green'}], hash = {};
// function to create a hashtable
function classify(e) {
if(hash[e.fruit] || hash[e.fruit_name]) {
Object.keys(e).forEach(function(c){
hash[e.fruit || e.fruit_name][c] = e[c];
});
} else {
hash[e.fruit_name || e.fruit] = e;
}
}
// add to hash
obj1.forEach(classify);
obj2.forEach(classify);
// extract the result
var obj3 = Object.keys(hash).map(function(e){
delete hash[e]['fruit'];
return hash[e];
});
console.log(obj3);
&#13;
.as-console-wrapper{top:0;max-height:100%!important;}
&#13;
答案 3 :(得分:0)
let obj1 = [{fruit: 'watermelon', sweetness: 3},{fruit: 'banana', sweetness: 4},{fruit: 'apple', sweetness: 5}];
let obj2 = [{fruit_name: 'apple', color: 'red'},{fruit_name: 'banana', color:'yellow'},{fruit_name: 'watermelon', color:'green'}];
function regulate(a) {
return a.map(v => {
if (v.fruit) {
v.fruit_name = v.fruit;
delete v.fruit;
}
return v;
});
}
let a = [].concat(regulate(obj1), regulate(obj2));
let merged = [];
a.reduce((m, v) => {
let f = m.filter(v2 => v2.fruit_name === v.fruit_name);
if (f.length > 0) {
Object.assign(f[0], v);
} else {
m.push(v);
}
return m;
}, merged);
console.log(merged);
答案 4 :(得分:0)
将两个对象数组与共享密钥合并在一起的通用函数相当简单。最简单的方法是使用基于关联数组的映射,如下所示。请注意,您可以使用此例程来解决类似类型的任何问题,但它肯定适用于您的数据 - 请参阅最后链接的JSFiddle。
(ETA:共享密钥只添加一次,名称提供为key1;如果您希望第二个密钥在输出中结束,只需将参数对交换到函数。)
obj1 = [{fruit: 'watermelon', sweetness: 3},{fruit: 'banana', sweetness: 4},{fruit: 'apple', sweetness: 5}];
obj2 = [{fruit_name: 'apple', color: 'red'},{fruit_name: 'banana', color:'yellow'},{fruit_name: 'watermelon', color:'green'}];
function mergeObjectArrays(array1, key1, array2, key2) {
var map = []; // an associative array/hashtable
var arrayValue, mapValue, propertyNames, propertyName, propertyValue;
// 1. Loop over one array, populating the map by each object's specified key
for(var x = 0; x < array1.length; x++) {
array1Value = array1[x];
map[array1Value[key1]] = array1Value;
map.push(array1Value);
}
// 2. Loop over the other array, matching on the provided keys
for(var x = 0; x < array2.length; x++) {
arrayValue = array2[x];
mapValue = map[arrayValue[key2]];
if (typeof(mapValue) != 'undefined') { // add all missing non-keyed properties to the mapped/merged object
propertyNames = Object.keys(arrayValue);
for (var y = 0; y < propertyNames.length; y++) {
propertyName = propertyNames[y];
if (propertyName != key1 && propertyName != key2) { // .. as that shared value is already added
propertyValue = arrayValue[propertyName];
mapValue[propertyName] = propertyValue;
}
}
}
else { // it's missing from the mapping, so at least add it though it will be missing array1 data
map[arrayValue[key2]] = arrayValue;
map.push(arrayValue);
}
}
return map;
}
var mergedArrays = mergeObjectArrays(obj1, 'fruit', obj2, 'fruit_name');