比较数组项

时间:2018-10-29 17:43:58

标签: javascript arrays comparison

我得到了一些具有两个属性的对象(someObject)的数组(someObjects)-数字和日期。我想比较数组中的所有对象。如果数组中存在对象的数目和最新日期比其他对象少-我需要返回“不好”。

如何用JavaScript做到这一点?

1 个答案:

答案 0 :(得分:0)

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