Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在两个对象数组之间匹配特定属性,然后将值插入一个对象_Javascript_Arrays - Fatal编程技术网

Javascript 在两个对象数组之间匹配特定属性,然后将值插入一个对象

Javascript 在两个对象数组之间匹配特定属性,然后将值插入一个对象,javascript,arrays,Javascript,Arrays,我有两个对象数组,比如- var arrayOne = [{"Content":1, "ValueContent":2},{"Content":3, "ValueContent":4}] var arrayTwo = [{"Ex": "x", "ValueNum":20,"Content_Key":3}, {"Ex":"y","ValueNum":10,"Content_Key": 1}] 我想将Content值从arrayOne匹配到Content\u键arrayTwo中的值。如果它与arr

我有两个对象数组,比如-

var arrayOne = [{"Content":1, "ValueContent":2},{"Content":3, "ValueContent":4}]
var arrayTwo = [{"Ex": "x", "ValueNum":20,"Content_Key":3}, {"Ex":"y","ValueNum":10,"Content_Key": 1}]
我想将
Content
值从
arrayOne
匹配到
Content\u键
arrayTwo
中的值。如果它与
arrayOne
中的
ValueContent
的更新
arrayTwo
匹配。所以更新后的数组看起来像-

 var arrayTwo = [{"Ex": "x", "ValueNum":20,"Content_Key":3, "ValueContent":4}, {"Ex":"y","ValueNum":10,"Content_Key": 1, "ValueContent":2}]

关于如何做到这一点有什么线索吗?目前,我能够找到匹配的部分,但除此之外,我被卡住了。

您可以为
arrayOne
使用哈希表,并将
ValueContent
应用到适当的对象(如果存在)

var arrayOne=[{“Content”:1,“ValueContent”:2},{“Content”:3,“ValueContent”:4}],
arrayTwo=[{“Ex”:“x”,“ValueNum”:20,“Content_Key”:3},{“Ex”:“y”,“ValueNum”:10,“Content_Key”:1}],
hash=Object.create(null);
arrayOne.forEach(函数(a){
散列[a.Content]=a;
});
arrayTwo.forEach(函数(a){
if(散列[a.Content\u Key]){
a、 ValueContent=hash[a.Content\u Key]。ValueContent;
}
});
控制台日志(arrayTwo)

.as console wrapper{max height:100%!important;top:0;}
您可以为
arrayOne
使用哈希表,并将
ValueContent
应用于适当的对象(如果存在)

var arrayOne=[{“Content”:1,“ValueContent”:2},{“Content”:3,“ValueContent”:4}],
arrayTwo=[{“Ex”:“x”,“ValueNum”:20,“Content_Key”:3},{“Ex”:“y”,“ValueNum”:10,“Content_Key”:1}],
hash=Object.create(null);
arrayOne.forEach(函数(a){
散列[a.Content]=a;
});
arrayTwo.forEach(函数(a){
if(散列[a.Content\u Key]){
a、 ValueContent=hash[a.Content\u Key]。ValueContent;
}
});
控制台日志(arrayTwo)

.as console wrapper{max height:100%!important;top:0;}
首先,我将在
数组two
中创建一个对象索引,其中键将是
Content\u键。也就是说,我将构建一个属性为
Content\u Key
的对象,并对整个数组中的对象进行赋值

因此,我可以通过
Content\u键
轻松定位对象

最后,我将迭代
arrayOne
中的每个项目,使用整个对象索引向它们添加
ValueContent
属性:

var arrayOne=[{“Content”:1,“ValueContent”:2},{“Content”:3,“ValueContent”:4}];
var arrayTwo=[{“Ex”:“x”,“ValueNum”:20,“Content_Key”:3},{“Ex”:“y”,“ValueNum”:10,“Content_Key”:1}];
var arrayTwoIndex=arrayTwo.reduce(函数(结果,项){
结果[项目内容\关键]=项目;
返回结果;
}, {});
arrayOne.forEach(功能(项目){
//这种使用对象索引的方法非常强大,因为
//否则,您需要为每个阵列迭代arrayTwo
//迭代,这样可以节省大量的CPU周期,因为
//您可以通过内容键直接访问阵列两个对象!
arrayTwoIndex[item.Content].ValueContent=item.ValueContent;
});

log(JSON.stringify(arrayTwo))
首先,我将在
数组two
中创建一个对象索引,其中键将是
内容键
。也就是说,我将构建一个属性为
Content\u Key
的对象,并对整个数组中的对象进行赋值

因此,我可以通过
Content\u键
轻松定位对象

最后,我将迭代
arrayOne
中的每个项目,使用整个对象索引向它们添加
ValueContent
属性:

var arrayOne=[{“Content”:1,“ValueContent”:2},{“Content”:3,“ValueContent”:4}];
var arrayTwo=[{“Ex”:“x”,“ValueNum”:20,“Content_Key”:3},{“Ex”:“y”,“ValueNum”:10,“Content_Key”:1}];
var arrayTwoIndex=arrayTwo.reduce(函数(结果,项){
结果[项目内容\关键]=项目;
返回结果;
}, {});
arrayOne.forEach(功能(项目){
//这种使用对象索引的方法非常强大,因为
//否则,您需要为每个arrayOne迭代arrayTwo
//迭代,这样可以节省大量的CPU周期,因为
//您可以通过内容键直接访问阵列两个对象!
arrayTwoIndex[item.Content].ValueContent=item.ValueContent;
});
log(JSON.stringify(arrayTwo))
尝试这种简单的方法(将复杂性降低到
O(n+m)
,其中
n
arrayOne
中的项数,
m
arrayTwo
中的项数)

var arrayOne=[{“Content”:1,“ValueContent”:2},{“Content”:3,“ValueContent”:4}];
var arrayTwo=[{“Ex”:“x”,“ValueNum”:20,“Content_Key”:3},{“Ex”:“y”,“ValueNum”:10,“Content_Key”:1}];
//从arrayone为内容值准备映射
var-map={};
arrayOne.forEach(功能(项目){
映射[项目[“内容”]=项目[“价值内容”];
});
//现在迭代arrayTwo以查看是否存在匹配项
arrayTwo=arrayTwo.map(函数(项){
var contentKey=item[“Content_Key”];
if(映射[contentKey]!=未定义)
{
项目[“ValueContent”]=映射[contentKey];
}
退货项目;
});
控制台日志(arrayTwo)
尝试这种简单的方法(将复杂性降低到
O(n+m)
,其中
n
arrayOne
中的项数,
m
arrayTwo
中的项数)

var arrayOne=[{“Content”:1,“ValueContent”:2},{“Content”:3,“ValueContent”:4}];
var arrayTwo=[{“Ex”:“x”,“ValueNum”:20,“Content_Key”:3},{“Ex”:“y”,“ValueNum”:10,“Content_Key”:1}];
//从arrayone为内容值准备映射
var-map={};
arrayOne.forEach(功能(项目){
映射[项目[“内容”]=项目[“价值内容”];
});
//现在迭代arrayTwo以查看是否有匹配项
arrayTwo=arrayTwo.map(函数(项){
var contentKey=item[“Content_Key”];
if(映射[contentKey]!=未定义)
{
项目[“ValueContent”]=映射[contentKey];
}
退货项目;
});
控制台日志(arrayTwo)你可以
arrayTwo.forEach(obj2 => { arrayOne.forEach(obj1 => {
if (obj2.Content_Key === obj1.Content)  obj2.ValueContent = obj1.ValueContent;
})});
console.log(arrayTwo); //[ { Ex: 'x', ValueNum: 20, Content_Key: 3, ValueContent: 4 }, { Ex: 'y', ValueNum: 10, Content_Key: 1, ValueContent: 2 } ]
arrayTwo.forEach(function (el) {
    el.ValueContent = this.get(el.Content_Key);
}, new Map(arrayOne.map(el => [el.Content, el.ValueContent])));