【问题标题】:Compare array items比较数组项
【发布时间】:2018-10-29 17:43:58
【问题描述】:

我得到了一些对象(someObject)的数组(someObjects),它有两个属性——数字和日期。我想将数组中的所有对象相互比较。如果数组中存在比其他对象更小的数字和最新日期的对象 - 我需要返回“not ok”。

如何在 JavaScript 中做到这一点?

【问题讨论】:

  • 如果您发布了您的数组示例和您想要的结果示例(以及您尝试过的任何内容),这将有所帮助。

标签: javascript arrays comparison


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    • 2018-09-18
    • 2016-01-24
    • 2021-11-28
    • 2018-12-03
    • 2011-02-07
    • 2019-02-01
    相关资源
    最近更新 更多