I need to find key of "1"s in the data below based on each bit of binaryInput. I have posted similar question before. But I am stuck on getting exact keys as result.
I have data as below:
item on -> 0
item off -> 1
item detected -> 0
item not detected-> 1
item responded-> 0
item not responded-> 1
item passed -> 0
item failed -> 1
Example:
lets take binaryInput= '1100'
var reversedInput= binaryInput.split("").reverse(); // '0011'
result =[];
Now first bit of reversedInput which is '0', need to return whether item on or item off. Second bit '0' need to return whether item detected or not detected. Third bit '1'would return item responded or not responded. fourth bit '1' return item passed or failed.
I am expecting the result to be ["item not responded", " item failed"] for binaryInput '1100' (reading from right). As we are adding to the result only that are "1"
Another eg: if binaryInput was '0010' (reading from right), result would be ["item not detected"].
Tried as below but not effective: Tried storing in the below data structure:
const data = {
"item on" : "0",
"item off" : "1",
"item detected" : "0",
"item not detected" : "1",
"item responded" : "0",
"item not responded" : "1",
"item passed" : "0",
"item failed" : "1",
}
function getKeyByValue(object, value) {
return Object.keys(object).find(key => object[key] === value);
}
for (var i = 0; i < reversedInput.length; i++) {
//getKeyByValue will only return 'item on' or 'item off' but not keys of other '0' or '1'. Since values are not unique.
var status= getKeyByValue(data, reversedInput[i]);
if (reversedInput[i] === "1") {
result.push(status);
}
Thank you.