0

I try to find the highest key value of 'sequence' where it's value is 'true'. I know it's not sql, but I would like know if it's possible to do this request on javascript.

For example, in my case I would like to have : 5 because "70" it's the highest value with bug_tab is true.

Here my js array 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"
  }
]

What I have do :

This return the first occurence where bug_tab is equal to true :

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

Advance thanks,

MasterSinge
  • 665
  • 3
  • 9
  • 22

2 Answers2

2

It can be done like that, maybe it's not the most efficient way but it works as expected

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]);
Nicolas Menettrier
  • 1,647
  • 1
  • 13
  • 28
2

Use array.reduce to get the highest index.

Also, as noted by another user, the correct answer based on your description is 4, not 5 like you said.

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);
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116