简体   繁体   English

比较数组项

[英]Compare array items

I got array (someObjects) of some objects (someObject) with two properties - number and date.我得到了一些具有两个属性的对象 (someObject) 的数组 (someObjects) - 数字和日期。 I want to compare all objects in array with each others.我想将数组中的所有对象相互比较。 If in array exist object with smaller number and latest date than other objects - I need to return "not ok".如果数组中存在编号和最新日期小于其他对象的对象 - 我需要返回“not ok”。

How can I do that in JavaScript?我如何在 JavaScript 中做到这一点?

 const filterFunc = (a, _, array) => { return (array.some(el => el.number > a.number) && array.some(el => el.date < a.date)); }; const isNotOk = array => { if (array.some(filterFunc)) console.log("not ok"); }; const someObjects = [{ number: 42, date: new Date(999999999999) }, { number: 7, date: new Date(555555555555) }]; const someObjects2 = [{ number: 1, date: new Date(999999999999) }, { number: 7, date: new Date(555555555555) }]; isNotOk(someObjects); // prints "not Ok" isNotOk(someObjects2); // does nothing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM