If I do:
var arr=Uint8Array(1);
arr[0]="hi";
console.log(arr[0]);
The result is 0
instead of "hi"
. That’s because that type of object (Uint8Array
) only admits 8-bit unsigned integers as values for its items.
Is there some type of array-like object made to store only boolean (true
/false
) values?
How can I create an object of that type?
Note: Please, don’t use watch
; it’s not cross-browser. I’m going to use it in any browser.
If I do:
var arr=Uint8Array(1);
arr[0]="hi";
console.log(arr[0]);
The result is 0
instead of "hi"
. That’s because that type of object (Uint8Array
) only admits 8-bit unsigned integers as values for its items.
Is there some type of array-like object made to store only boolean (true
/false
) values?
How can I create an object of that type?
Note: Please, don’t use watch
; it’s not cross-browser. I’m going to use it in any browser.
-
1
var arr = [true, false, true];
doesn't work for you? – Oleg Commented Apr 29, 2015 at 20:08 - 1 var arr=[true,false,true]; arr[0]="test"; console.log(arr); Try that. – t33st33r Commented Apr 29, 2015 at 20:10
-
1
It returns
["test", false, true]
. What's the problem? – Oleg Commented Apr 29, 2015 at 20:12 - 1 "test" is not a boolean value. The idea is to make an array able to store boolean values exclusively. – t33st33r Commented Apr 29, 2015 at 20:14
- 2 Have you looked at stackoverflow./a/6973312/123415? – jcuenod Commented Apr 29, 2015 at 20:21
5 Answers
Reset to default 1This isn't exactly what i asked for, but it could be a good implementation:
Uint8Array.prototype.getBitSum=function() {
var _t=0;
[].forEach.call(this, function(v, i) { _t+=Math.pow(256,i)*v; });
return _t;
}
Uint8Array.prototype.getBit=function(b) {
var _t=this.getBitSum();
return ((_t & Math.pow(2,b)) != false);
}
Uint8Array.prototype.setBit=function(b, v) {
var _v=v;
if (isNaN(_v)) return false;
var _B=Math.floor(b/8), _B_b=(b%8);
if (_v) this[_B]|=Math.pow(2,_B_b);
else this[_B]&=(255-Math.pow(2,_B_b));
return true;
}
My limited knowledge about some terms made of this a hard work for me. If you find some bug... add ment, please.
The reason of this is to minimize memory reservation. So I think it does right.
You cannot have boolean-only arrays natively in JavaScript. Especially with legacy browsers support. Check the type of a value before inserting it into your array. If it's not a boolean, i.e. typeof x === 'boolean'
then don't insert it.
The only types supported for JavaScript typed arrays are the following:
- Int8Array
- Uint8Array
- Uint8ClampedArray
- Int16Array
- Uint16Array
- Int32Array
- Uint32Array
- Float32Array
- Float64Array
For anything else, you'd have to use standard JavaScript arrays like this:
var a = [true];
a[0] = false;
console.log(a); // false
Note that JavaScript being a dynamically typed language will let the developer also put values of other types into the array (such as "hi"
). You can enforce types using functions that modify the array for the user after double checking the type:
function set(index, value) {
if (typeof value != 'boolean') {
throw new Error('Expected a boolean');
}
a[index] = value;
}
No, there's not. Here's the MDN documentation on typed arrays in JS and you can see there's only typed arrays for various sizes of integers and floats.
As for why there's no "boolean typed arrays"? Well, I think the primary purpose of typed arrays in JS is for efficiently handling large amounts of data for APIs, and there's really not a ton of demand for APIs to take a large amount of boolean data.
In short, the primary purpose is performance, not type-safety. The purpose of typed arrays isn't really to prevent the wrong type of data from being added to the array.
But, if you really want a "boolean array", you could write a wrapper around a UInt8Array that stores and reads booleans, using bitwise operations to store the booleans as individual bits. (i.e. something like this answer, though backed by a typed array) But I'd question the value in doing so.
You are asking for a bitset in JavaScript. There are libraries implementing bitsets in JavaScript, see for example :
https://github./lemire/FastBitSet.js