[英]Update an array of objects with a different array of objects data
I have a very tricky manipulation to do. 我有一个非常棘手的操作。 So here it is.
就是这样
I have an array of Objects named 'data1' 我有一个名为“ data1”的对象数组
[Object, Object, Object]
0:Object
id="00456145" //check this
name: "Rick"
upper:"0.67"
lower:"-0.34"
1:Object
id="00379321"
name:"Anjie"
upper:"0.46"
lower:"-0.56"
2:Object
id="00323113"
name:"dillan"
upper:"0.23"
lower:"-0.11"
I am only interested in id, upper and lower values from these array of objects. 我只对这些对象数组的id,上限值和下限值感兴趣。
Here is the second array of objects named 'data2' 这是名为“ data2”的对象的第二个数组
[Object, Object]
0:Object
id="0045614" //present here if we remove last element of '00456145'
cast="Rick"
Contact: "Yes"
upper:"0.11" //need to be updated to '0.67'
lower:"-0.11" //need to be updated to '-0.34'
1:Object
id="0032311" //present here if we remove last element of '00323113'
cast:"dillan"
Contact:"Maybe"
upper:"0.11"
lower:"-0.11"
So, this is what i have to do. 所以,这就是我要做的。 I will first check 'data1'.
我将首先检查“ data1”。 id present in 'data1' is checked.
检查“ data1”中存在的ID。 For eg object 0 has id="00456145"
例如,对象0具有id =“ 00456145”
I remove the last number present in it. 我删除其中的最后一个数字。 so it becomes id="0045614".
因此它变为id =“ 0045614”。 Then i compare if this id is present in the object 'data2'.
然后,我比较此id是否存在于对象“ data2”中。
If it is present, the upper and lower values present for that object 0 in 'data1' is passed to object 'data2' where the id is present. 如果存在,则在“ data1”中为该对象0显示的上限值和下限值传递到存在id的对象“ data2”中。 In this case object 0 of 'data2' has the id= '0045614'.
在这种情况下,“ data2”的对象0具有id =“ 0045614”。
So upper and lower values will be updated to 0.67 and -0.34 respectively. 因此上限和下限值将分别更新为0.67和-0.34。
Similarly for other arrays. 对于其他阵列也是如此。 So the final output should look like this of 'data2'
因此最终输出应类似于“ data2”
[Object, Object]
0:Object
id="0045614"
cast="Rick"
Contact: "Yes"
upper:"0.67" //updated
lower:"-0.34" //updated
1:Object
id="0032311"
cast:"dillan"
Contact:"Maybe"
upper:"0.23" //updated
lower:"-0.11" //updated
I like array#some in this case, so that you can get out of the loop so there are no unnecessary iterations: 在这种情况下,我喜欢array#some ,这样您就可以摆脱循环,因此不会出现不必要的迭代:
var data1 = [{ id: "00456145", name: "Rick", upper: "0.67", lower: "-0.34" }, { id: "00379321", name: "Anjie", upper: "0.46", lower: "-0.56" }, { id: "00323113", name: "dillan", upper: "0.23", lower: "-0.11" }]; var data2 = [{ id: "0045614", cast: "Rick", Contact: "Yes", upper: "0.11", lower: "-0.11" }, { id: "0032311", cast: "dillan", Contact: "Maybe", upper: "0.11", lower: "-0.11" }]; data2 = data2.map(function(item) { data1.some(function(a) { if (item.id == a.id.slice(0, -1)) { item.upper = a.upper; item.lower = a.lower; return true; } }); return item; }); console.log(data2);
I suggest to use either an object as hash table or a map. 我建议使用一个对象作为哈希表或映射。 Then only two loops are necessary, one for getting the references and one for the assignments.
然后,仅需要两个循环,一个循环用于获取引用,一个循环用于分配。
Big O: O(n + m) 大O: O(n + m)
While I actually not know which one (Map vs Object) is more suited, you may get your own picture: 虽然我实际上不知道哪一种(地图与对象)更适合,但您可能会得到自己的照片:
Proposal with Object
带
Object
提案
var data1 = [{ id: "00456145", name: "Rick", upper: "0.67", lower: "-0.34", }, { id: "00379321", name: "Anjie", upper: "0.46", lower: "-0.56", }, { id: "00323113", name: "dillan", upper: "0.23", lower: "-0.11" }], data2 = [{ id: "0045614", cast: "Rick", Contact: "Yes", upper: "0.11", lower: "-0.11", }, { id: "0032311", cast: "dillan", Contact: "Maybe", upper: "0.11", lower: "-0.11" }], hash = Object.create(null); data1.forEach(function (a) { hash[a.id.slice(0, -1)] = a; }); data2.forEach(function (a) { var o = hash[a.id]; o && Object.keys(o).forEach(function (k) { if (k !== 'id' && a[k] !== o[k]) { a[k] = o[k]; } }); }); console.log(data2);
var data1 = [{ id: "00456145", name: "Rick", upper: "0.67", lower: "-0.34", }, { id: "00379321", name: "Anjie", upper: "0.46", lower: "-0.56", }, { id: "00323113", name: "dillan", upper: "0.23", lower: "-0.11" }], data2 = [{ id: "0045614", cast: "Rick", Contact: "Yes", upper: "0.11", lower: "-0.11", }, { id: "0032311", cast: "dillan", Contact: "Maybe", upper: "0.11", lower: "-0.11" }], map = new Map; data1.forEach(function (a) { map.set(a.id.slice(0, -1), a); }); data2.forEach(function (a) { var o = map.get(a.id); o && Object.keys(o).forEach(function (k) { if (k !== 'id' && a[k] !== o[k]) { a[k] = o[k]; } }); }); console.log(data2);
You can try something like that: 您可以尝试类似的方法:
for (var i = 0; i < data1.length; i++) {
var item = data1[i];
var trim = item.id.substr(0, item.id.length - 1);
for (var j = 0; j < data2.length; j++) {
var item2 = data2[j];
if (item2.id === trim) {
item2.upper = item.upper;
item2.lower = item.lower;
}
}
}
and if you are using es6 you can simplify the code by: 如果您使用的是es6,则可以通过以下方式简化代码:
for (let item of data1) {
let trim = item.id.substr(0, item.id.length - 1);
for (let item2 of data2) {
if (item2.id === trim) {
item2.upper = item.upper;
item2.lower = item.lower;
}
}
}
Here is a solution using Array.prototype.forEach
. 这是使用
Array.prototype.forEach
的解决方案。 Loop through data2
objects and if id matches with any object in data1
, update values with matching one. 遍历
data2
对象,如果id与data1
任何对象匹配,则用匹配的对象更新值。
var data1 = [{
id: "00456145",
name: "Rick",
upper: "0.67",
lower: "-0.34"
}, {
id: "00379321",
name: "Anjie",
upper: "0.46",
lower: "-0.56"
}, {
id: "00323113",
name: "dillan",
upper: "0.23",
lower: "-0.11"
}];
var data2 = [{
id: "0045610", //present here if we remove last element of '00456145'
cast: "Rick",
Contact: "Yes",
upper: "0.11", //need to be updated to '0.67'
lower: "-0.11" //need to be updated to '-0.34'
}, {
id: "0032311", //present here if we remove last element of '00323113'
cast: "dillan",
Contact: "Maybe",
upper: "0.11",
lower: "-0.11"
}];
data2.forEach(function(item2){
data1.forEach(function(item1){
if (item1.id.substring(0,7) === item2.id){ // can be `item1.id.indexOf(item2.id) == 0
item2.upper = item1.upper;
item2.lower = item1.lower;
}
});
});
console.log(data2);
You can use something like 您可以使用类似
data1.forEach(function(obj) {
var search = obj.id.slice(0, -1);
data2.forEach(function(d) {
if (d.id === search) {
d.upper = obj.upper;
d.lower = obj.lower;
};
});
});
to do this. 去做这个。
Maybe this is what you want. 也许这就是您想要的。
var data1 = [{ id: "00456145", name: "Rick", upper: "0.67", lower: "-0.34" }, { id: "00379321", name: "Anjie", upper: "0.46", lower: "-0.56" }, { id: "00323113", name: "dillan", upper: "0.23", lower: "-0.11" }]; var data2 = [{ id:"0045614", cast:"Rick", Contact: "Yes", upper:"0.11", lower:"-0.11", }, { id:"0032311", cast:"dillan", Contact:"Maybe", upper:"0.11", lower:"-0.11", }]; data2.forEach(function(data2Value) { data1.forEach(function(data1Value) { if(data1Value.id.substr(0, data1Value.id.length - 1) === data2Value.id) { data2Value.upper = data1Value.upper; data2Value.lower = data1Value.lower; } }); }); console.log(data2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.