javascript - 搜索最高值和特定值的键
问题描述
我试图找到'sequence'的最高键值,它的值为'true'。我知道这不是 sql,但我想知道是否可以在 javascript 上执行此请求。
例如,在我的情况下,我想要: 5 因为“70”它是最高值,而 bug_tab 为真。
这是我的 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);
提前谢谢,
解决方案
可以这样做,也许这不是最有效的方式,但它可以按预期工作
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]);
推荐阅读
- javascript - 用 2 个值计算 3 个不同的金额
- salesforce - 如何将相关记录添加到闪电页面布局
- node.js - 与键关联的 Firebase 实时数据库访问值
- css - 使用 webpack 4 处理 font-awesome 的问题 - React JS
- r - 使用 sed 将字符串动态传递给 Rscript 参数
- ejabberd - ejabberd - 基于外部 REST API 的自定义花名册模块
- laravel - lodash 函数的 npm 生产构建失败,而 dev 构建很好
- python - glVertexAttribPointer() 上的 pyopengl 错误
- reactjs - 从 componentDidMount 导出变量
- android - How to Create Searching with ListView in Flutter?