【问题标题】:Search the key of the highest value and specific value [duplicate]搜索最高值和特定值的键[重复]
【发布时间】:2021-08-19 12:43:32
【问题描述】:

我试图找到'sequence'的最高键值,它的值为'true'。 我知道这不是 sql,但我想知道是否可以在 javascript 上执行此请求。

例如,在我的情况下,我想要:5,因为“70”它是最高值,而 bug_tab 为 true。

这是我的 js 数组 myTab :

[
  {
    "value": "AHAH",
    "field": "15",
    "color": "",
    "bug_tab": true,
    "sequence": "40",
    "text": "slash"
  },
  {
    "value": "BABA",
    "field": "8",
    "color": "",
    "bug_tab": true,
    "sequence": "50",
    "text": "zip"
  },
  {
    "value": "CACA",
    "field": "25",
    "color": "",
    "bug_tab": false,
    "sequence": "63",
    "text": "vite"
  },
  {
    "value": "DADA",
    "field": "22",
    "color": "",
    "bug_tab": true,
    "sequence": "66",
    "text": "meat"
  },
  {
    "value": "EVA",
    "field": "13",
    "color": "",
    "bug_tab": true,
    "sequence": "70",
    "text": "zut"
  },
  {
    "value": "FAFA",
    "field": "jut",
    "color": "",
    "bug_tab": false,
    "sequence": "90",
    "text": "cut"
  }
]

我做了什么:

这会返回 bug_tab 等于 true 的第一次出现:

var indexbugTabArray = myTab.map(function(o) { return o.bug_tab; }).indexOf(true);

提前谢谢,

【问题讨论】:

  • 您能否将您尝试的代码添加到minimal reproducible example 的问题中?
  • 相关问题没有提到找到索引(也许这是一个微不足道的区别,但它仍然是一个区别)

标签: javascript arrays


【解决方案1】:

使用array.reduce 获取最高索引。

另外,正如另一位用户所说,根据您的描述,正确答案是 4,而不是您所说的 5。

var arr = [{
    "value": "AHAH",
    "field": "15",
    "color": "",
    "bug_tab": true,
    "sequence": "40",
    "text": "slash"
  },
  {
    "value": "BABA",
    "field": "8",
    "color": "",
    "bug_tab": true,
    "sequence": "50",
    "text": "zip"
  },
  {
    "value": "CACA",
    "field": "25",
    "color": "",
    "bug_tab": false,
    "sequence": "63",
    "text": "vite"
  },
  {
    "value": "DADA",
    "field": "22",
    "color": "",
    "bug_tab": true,
    "sequence": "66",
    "text": "meat"
  },
  {
    "value": "EVA",
    "field": "13",
    "color": "",
    "bug_tab": true,
    "sequence": "70",
    "text": "zut"
  },
  {
    "value": "FAFA",
    "field": "jut",
    "color": "",
    "bug_tab": false,
    "sequence": "90",
    "text": "cut"
  }
];

var res = arr.reduce((acc, curr, idx, arr) => 
  curr.bug_tab && +curr.sequence > +arr[acc].sequence ? idx : acc 
, 0);

console.log(res);

【讨论】:

  • 本来打算用reduce写的,但是没找到方法,真的很好用!
【解决方案2】:

可以这样做,也许这不是最有效的方式,但它可以按预期工作

const toto = [
  {
    "value": "AHAH",
    "field": "15",
    "color": "",
    "bug_tab": true,
    "sequence": "40",
    "text": "slash"
  },
  {
    "value": "BABA",
    "field": "8",
    "color": "",
    "bug_tab": true,
    "sequence": "50",
    "text": "zip"
  },
  {
    "value": "CACA",
    "field": "25",
    "color": "",
    "bug_tab": false,
    "sequence": "63",
    "text": "vite"
  },
  {
    "value": "DADA",
    "field": "22",
    "color": "",
    "bug_tab": true,
    "sequence": "66",
    "text": "meat"
  },
  {
    "value": "EVA",
    "field": "13",
    "color": "",
    "bug_tab": true,
    "sequence": "70",
    "text": "zut"
  },
  {
    "value": "FAFA",
    "field": "jut",
    "color": "",
    "bug_tab": false,
    "sequence": "90",
    "text": "cut"
  }
];

const max = {
  index: -1, // -1 so you can check if you find one
  value: 0,
};

toto.forEach((el, index) => {
  if (+el.sequence > max.value && el.bug_tab) {
    max.index = index;
    max.value = +el.sequence;
  }
});

console.log(max.index, max.value, toto[max.index]);

【讨论】:

  • OP 要求的是索引,而不是对象。他想要的答案是 5
  • @Iwrestledabearonce。他想要的答案是带有 bug_tab === true 的最大序列值的索引,即 4,之后他可以使用 max.index 。我不明白你的评论。我什至没有将对象存储在我的代码中。
  • 是的。我的错。显然,OP 的错误也是如此。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-10
  • 1970-01-01
  • 2015-02-18
  • 2021-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多