如何解决使用嵌套的for循环比较两个对象的值,并返回修改后的对象的数组
我有两个对象数组:
const testArr1 = [
{
event: "Ryan's birthday",time: "4:00",},{
event: "Steves's birthday",time: "2:00",{
event: "Helen's birthday",time: "1:00",{
event: "Paola's birthday",time: "3:00",{
event: "Jared's birthday",time: "9:00",];
和
const testArr2 = [
{
time: "4:00",temp: 41,{
time: "6:00",temp: 42,{
time: "8:00",temp: 43,{
time: "1:00",temp: 44,{
time: "3:00",temp: 45,{
time: "9:00",temp: 46,];
我想创建一个函数,该函数接受这两个对象数组并返回一个名为dataToDisplay
的新对象数组。 dataToDisplay
应包含与事件相关的对象。
我希望函数getDataToDisplay
通过比较testArr1
键上的testArr2
和time
值来查看事件是否有温度数据,然后给我一个数组在temp
值匹配时具有相关time
数据的对象,或者在no matching data found
值不匹配时具有time
的对象。
我的问题是,使用现在构建的函数,对象会在每次迭代中被推入dataToDisplay
,在比较时间值时返回false,所以有很多重复的数据。
功能如下:
const getDataToDisplay = (testArr1,testArr2) => {
const dataToDisplay = [];
for (let i = 0; i < testArr1.length; i++) {
for (let j = 0; j < testArr2.length; j++) {
let objToPush = {};
//if times in both objects are equal,push an object containing event,time,and temp value at that time to dataToDisplay
//break and move on if times are equal
if (testArr1[i].time === testArr2[j].time) {
Object.assign(objToPush,{
event: testArr1[i].event,time: testArr1[i].time,temp: testArr2[j].temp,});
dataToDisplay.push(objToPush);
break;
} else {
//if times in both objects are NOT equal,and a temp value of "no matching data found"
Object.assign(objToPush,temp: "no matching data found",});
//this is pushing an object every time the condition returns false
//I only want one object returned if false
dataToDisplay.push(objToPush);
}
}
}
return dataToDisplay;
};
调用函数时会发生什么
console.log(getDataToDisplay(testArr1,testArr2));
//logs
// [
// {
// "event": "Ryan's birthday",// "time": "4:00",// "temperature": 41
// },// {
// "event": "Steves's birthday",// "time": "2:00",// "temperature": "no matching data found"
// },// "temperature": "no matching data found"
// }
// ...
// ]
//but I want it to log something like this
// [
// {
// event: "Ryan's birthday",// time: "4:00",// temperature: 41,// },// {
// event: "Steves's birthday",// time: "2:00",// temperature: "no matching data found",// {
// event: "Helen's birthday",// time: "1:00",// temperature: 44,// ...
// ];
如何仅为每个事件返回一个新的修改对象,而不是每个比较的结果?我觉得我好亲近!
解决方法
您可以一直使用temparatures
数组的对象,并使用events
作为键将times
对象的值映射到time
数组。
const
noData = 'no matching data found',events = [{ event: "Ryan's birthday",time: "4:00" },{ event: "Steves's birthday",time: "2:00" },{ event: "Helen's birthday",time: "1:00" },{ event: "Paola's birthday",time: "3:00" },{ event: "Jared's birthday",time: "9:00" }],temperatures = [{ time: "4:00",temp: 41 },{ time: "6:00",temp: 42 },{ time: "8:00",temp: 43 },{ time: "1:00",temp: 44 },{ time: "3:00",temp: 45 },{ time: "9:00",temp: 46 }],times = Object.fromEntries(temperatures.map(({ time,temp }) => [time,temp])),result = events.map(o => ({ ...o,temperature: times[o.time] || noData }));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
,
您可以使用Array.prototype.reduce
并遍历testArr1
中的每个对象,并使用temp
在testArr2
中找到与Array.prototype.find
相匹配的对象。如果匹配,则将两者都填充为“找不到匹配的记录”:
const testArr1 = [{ event: "Ryan's birthday",testArr2 = [{ time: "4:00",temp: 46 }];
const getDataToDisplay = (testArr1,testArr2) => {
const defaultText = "no matching records found";
return testArr1.reduce((acc,p) => {
const matchingTime = testArr2.find(t => t.time === p.time);
const temperature = (matchingTime && matchingTime.temp) || defaultText;
acc.push({...p,temperature});
return acc;
},[]);
}
console.log(getDataToDisplay(testArr1,testArr2));
您的方法存在的问题是,您正在将外部for循环中的对象与内部for循环中的每个对象进行比较。您只需在第二个数组testArr2
中找到一个具有相同时间的对象(如果找不到),则添加消息“未找到匹配的记录”。
但是在您的情况下,每次time
属性不匹配时,内部循环都会在输出数组中推送一个新对象。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。