Vladimir Vladimir - 3 months ago 10
Javascript Question

How to simplify the code?

Is there a better way to find values?

let arrData = [[1, 0], [74, 0], [75, 1], [76, 1], [79, 1], [80, 0], [81, 0], [82, 0], [83, 1], [84, 0], [85, 0], [86, 0]];
let isn = 75;

function searchCount(arr, isn) {
for(var i = 0; i < arr.length; i++){
if(arr[i][0] == isn){
return arr[i][1];
}
}
}

console.log(searchCount(arrData, isn));

Answer

You could also convert to a dictionary as below :

var dataMap = 
    arrData.reduce(function(p, c) { p[c[0]] = c[1]; return p; }, {});

You could use dataMap to search by passing the item to search as an index to dataMap:

dataMap[75]