猿问

根据最大值和日期将对象推送到新数组?在 JavaScript 中

我有一个包含公斤数和一些日期的对象。我想要做的是将具有唯一日期的行推送到新数组。如果有包含相同日期的行,则只应将千克最高的行推送到新数组。如果有两个相同公斤的日期,则只能选择一个。


这是它的样子:


[{"kg":10,"date":"14/10/2019"},

{"kg":15,"date":"14/10/2019"},

{"kg":15,"date":"14/10/2019"},

{"kg":5,"date":"15/10/2019"},

{"kg":10,"date":"16/10/2019"}]

以下是新数组中的结果:


{"kg":15,"date":"14/10/2019"},

{"kg":5,"date":"15/10/2019"},

{"kg":10,"date":"16/10/2019"}]

现在我不知道如何检查公斤并获得最高的公斤。我知道我可以使用“Math.max”来获得最高值,但不知道如何仅在相同日期使用它。我一直在尝试一些 foreach 循环,但我无法理解如何对日期进行分组并执行 Math.max。我真的需要一些指导。


注:语言为 Javascript。


慕婉清6462132
浏览 106回答 2
2回答

莫回无

您可以使用 Array#reduce 减少整个数组。const arr = [{"kg":10, "date": "14/10/2019"},{"kg":15, "date": "14/10/2019"},{"kg":15, "date": "14/10/2019"},{"kg":5, "date": "15/10/2019"},{"kg":10, "date": "16/10/2019"}];const o = Object.values(arr.reduce((s, a) => {&nbsp; &nbsp;if (s[a.date]) { // check if that date already exist in the result obj&nbsp; &nbsp; &nbsp; if (s[a.date].kg < a.kg) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s[a.date] = a; // assign a new value only if has higher "kg" than current&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;} else {&nbsp; &nbsp; &nbsp; s[a.date] = a; // if doesnt exist yet, assign it anyways to our obj&nbsp; &nbsp;}&nbsp; &nbsp;&nbsp; &nbsp;return s;}, {}));console.log(o);

一只萌萌小番薯

我认为您的问题的很大一部分是日期格式。尝试 10/14/2019 而不是 14/10/2019。之后,您始终可以通过创建日期对象并在该对象上使用 getDate() 方法来获取要比较的值,然后您就可以进行所需的比较。这是一个在单遍 for 循环中工作的简单示例。我首先对数据进行排序,以防您最终得到乱序的数据,但如果您确定日期始终按时间顺序排列,则可以将其删除。我能够一次性完成此操作的方法是假设数组按日期从最旧的开始和最新的日期排序。一定要记住数组对象是引用类型。如果您需要这些值,请确保制作足够深的副本。compressArray();function compressArray() {&nbsp; &nbsp; var orignialArray =&nbsp; &nbsp; [{ "kg": 12, date: new Date('10/17/2019') },&nbsp; &nbsp; { "kg": 11, date: new Date('10/14/2019') },&nbsp; &nbsp; { "kg": 15, date: new Date('10/14/2019') },&nbsp; &nbsp; { "kg": 5, date: new Date('10/15/2019') },&nbsp; &nbsp; { "kg": 15, date: new Date('10/16/2019') }];&nbsp; &nbsp; //first sort data in case the data is out of order&nbsp; &nbsp; orignialArray = orignialArray.sort((a, b) => a.date.getDate() - b.date.getDate());&nbsp; &nbsp; console.log(orignialArray); //after sorting&nbsp; &nbsp; var compressedArray = new Array();&nbsp; &nbsp; compressedArray.push(orignialArray[0]); // seed array&nbsp; &nbsp; let j = 0; // this will be my index of the "compressed" array&nbsp; &nbsp; for (let i = 0; i < orignialArray.length; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (orignialArray[i].date.getDate() == compressedArray[j].date.getDate()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; compressedArray[j].kg = Math.max(orignialArray[i].kg, compressedArray[j].kg);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j++; //move compressed array index&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; compressedArray[j] = orignialArray[i];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log("i: " + i + "and j: " + j);&nbsp; &nbsp; }&nbsp; &nbsp; console.log(compressedArray);}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答