Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

BooleanArray.

License

NotificationsYou must be signed in to change notification settings

stdlib-js/array-bool

About stdlib...

We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.

The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.

When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.

To join us in bringing numerical computing to the web, get started by checking us out onGitHub, and please considerfinancially supporting stdlib. We greatly appreciate your continued support!

BooleanArray

NPM versionBuild StatusCoverage Status

Boolean array.

Installation

npm install @stdlib/array-bool

Alternatively,

  • To load the package in a website via ascript tag without installation and bundlers, use theES Module available on theesm branch (seeREADME).
  • If you are using Deno, visit thedeno branch (seeREADME for usage intructions).
  • For use in Observable, or in browser/node environments, use theUniversal Module Definition (UMD) build available on theumd branch (seeREADME).

Thebranches.md file summarizes the available branches and displays a diagram illustrating their relationships.

To view installation and usage instructions specific to each branch build, be sure to explicitly navigate to the respective README files on each branch, as linked to above.

Usage

varBooleanArray=require('@stdlib/array-bool');

BooleanArray()

Creates a boolean array.

vararr=newBooleanArray();// returns <BooleanArray>

BooleanArray( length )

Creates a boolean array having a specifiedlength.

vararr=newBooleanArray(10);// returns <BooleanArray>varlen=arr.length;// returns 10

BooleanArray( booleanarray )

Creates a boolean array from another boolean array.

vararr1=newBooleanArray([true,false,false,true]);// returns <BooleanArray>vararr2=newBooleanArray(arr1);// returns <BooleanArray>varlen=arr2.length;// returns 4

BooleanArray( typedarray )

Creates a boolean array from atyped array.

varUint8Array=require('@stdlib/array-uint8');varbuf=newUint8Array([1,0,0,1]);// returns <Uint8Array>[ 1, 0, 0, 1 ]vararr=newBooleanArray(buf);// returns <BooleanArray>varlen=arr.length;// returns 4

BooleanArray( obj )

Creates a boolean array from an array-like object or iterable.

// From an array of booleans:vararr1=newBooleanArray([true,false,false,true]);// returns <BooleanArray>varlen=arr1.length;// returns 4// From an array containing non-booleans:vararr2=newBooleanArray([{},null,'',4]);len=arr2.length;// returns 4

BooleanArray( buffer[, byteOffset[, length]] )

Returns a boolean array view of anArrayBuffer.

varArrayBuffer=require('@stdlib/array-buffer');varbuf=newArrayBuffer(240);vararr1=newBooleanArray(buf);// returns <BooleanArray>varlen=arr1.length;// returns 240vararr2=newBooleanArray(buf,8);// returns <BooleanArray>len=arr2.length;// returns 232vararr3=newBooleanArray(buf,8,20);// returns <BooleanArray>len=arr3.length;// returns 20

Properties

BooleanArray.BYTES_PER_ELEMENT

Static property returning the size (in bytes) of each array element.

varnbytes=BooleanArray.BYTES_PER_ELEMENT;// returns 1

BooleanArray.name

Static property returning the constructor name.

varstr=BooleanArray.name;// returns 'BooleanArray'

BooleanArray.prototype.buffer

Pointer to the underlying data buffer.

vararr=newBooleanArray(2);// returns <BooleanArray>varbuf=arr.buffer;// returns <ArrayBuffer>

BooleanArray.prototype.byteLength

Size (in bytes) of the array.

vararr=newBooleanArray(10);// returns <BooleanArray>varnbytes=arr.byteLength;// returns 10

BooleanArray.prototype.byteOffset

Offset (in bytes) of the array from the start of its underlyingArrayBuffer.

varArrayBuffer=require('@stdlib/array-buffer');vararr=newBooleanArray(10);// returns <BooleanArray>varoffset=arr.byteOffset;// returns 0varbuf=newArrayBuffer(240);arr=newBooleanArray(buf,64);// returns <BooleanArray>offset=arr.byteOffset;// returns 64

BooleanArray.prototype.BYTES_PER_ELEMENT

Size (in bytes) of each array element.

vararr=newBooleanArray(10);// returns <BooleanArray>varnbytes=arr.BYTES_PER_ELEMENT;// returns 1

BooleanArray.prototype.length

Number of array elements.

vararr=newBooleanArray(10);// returns <BooleanArray>varlen=arr.length;// returns 10

Methods

BooleanArray.from( src[, clbk[, thisArg]] )

Creates a new boolean array from an array-like object or an iterable.

vararr=BooleanArray.from([true,false]);// returns <BooleanArray>varlen=arr.length;// returns 2

To invoke a function for eachsrc value, provide a callback function.

functionmap(v){return!v;}// Create a source array:varsrc=[true,false];// Create a new boolean array by inverting the source array:vararr=BooleanArray.from(src,map);// returns <BooleanArray>varlen=arr.length;// returns 2varv=arr.get(0);// returns falsev=arr.get(1);// returns true

A callback function is provided two arguments:

  • value: source value.
  • index: source index.

To set the callback execution context, provide athisArg.

functionmap(v){this.count+=1;return!v;}// Create a source array:varsrc=[true,false];// Define an execution context:varctx={'count':0};// Create a new boolean array by inverting the source array:vararr=BooleanArray.from(src,map,ctx);// returns <BooleanArray>varlen=arr.length;// returns 2varn=ctx.count;// returns 2

BooleanArray.of( element0[, element1[, ...elementN]] )

Creates a new boolean array from a variable number of arguments.

vararr=BooleanArray.of(true,false,false,true);// returns <BooleanArray>varlen=arr.length;// returns 4

BooleanArray.prototype.at( i )

Returns an array element located at integer position (index)i, with support for both nonnegative and negative integer positions.

vararr=newBooleanArray(3);arr.set(true,0);arr.set(false,1);arr.set(true,2);varv=arr.at(0);// returns truev=arr.at(-1);// returns true

If provided an out-of-bounds index, the method returnsundefined.

vararr=newBooleanArray(10);varv=arr.at(100);// returns undefinedv=arr.at(-100);// returns undefined

BooleanArray.prototype.copyWithin( target, start[, end] )

Copies a sequence of elements within the array starting atstart and ending atend (non-inclusive) to the position starting attarget.

vararr=newBooleanArray(4);arr.set(true,0);arr.set(false,1);arr.set(false,2);arr.set(true,3);varv=arr.get(0);// returns truev=arr.get(1);// returns false// Copy the last two elements to the first two elements:arr.copyWithin(0,2);v=arr.get(0);// returns falsev=arr.get(1);// returns true

By default,end equals the number of array elements (i.e., one more than the last array index). To limit the sequence length, provide anend argument.

vararr=newBooleanArray(4);arr.set(true,0);arr.set(false,1);arr.set(false,2);arr.set(true,3);varv=arr.get(2);// returns falsev=arr.get(3);// returns true// Copy the first two elements to the last two elements:arr.copyWithin(2,0,2);v=arr.get(2);// returns truev=arr.get(3);// returns false

When atarget,start, and/orend index is negative, the respective index is determined relative to the last array element. The following example achieves the same behavior as the previous example:

vararr=newBooleanArray(4);arr.set(true,0);arr.set(false,1);arr.set(false,2);arr.set(true,3);varv=arr.get(2);// returns falsev=arr.get(3);// returns true// Copy the first two elements to the last two elements using negative indices:arr.copyWithin(-2,-4,-2);v=arr.get(2);// returns truev=arr.get(3);// returns false

BooleanArray.prototype.entries()

Returns an iterator for iterating over array key-value pairs.

vararr=newBooleanArray(3);arr.set(true,0);arr.set(false,1);arr.set(true,2);varit=arr.entries();varv=it.next().value;// returns [ 0, true ]v=it.next().value;// returns [ 1, false ]v=it.next().value;// returns [ 2, true ]varbool=it.next().done;// returns true

The returnediterator protocol-compliant object has the following properties:

  • next: function which returns aniterator protocol-compliant object containing the next iterated value (if one exists) assigned to avalue property and adone property having aboolean value indicating whether theiterator is finished.
  • return: function which closes aniterator and returns a single (optional) argument in aniterator protocol-compliant object.

BooleanArray.prototype.every( predicate[, thisArg] )

Returns a boolean indicating whether all elements pass a test.

functionpredicate(v){returnv===true;}vararr=newBooleanArray(3);arr.set(true,0);arr.set(true,1);arr.set(true,2);varbool=arr.every(predicate);// returns true

Thepredicate function is provided three arguments:

  • value: current array element.
  • index: current array element index.
  • arr: the array on which this method was called.

To set the function execution context, provide athisArg.

functionpredicate(v){this.count+=1;returnv===true;}vararr=newBooleanArray(3);varcontext={'count':0};arr.set(true,0);arr.set(true,1);arr.set(true,2);varbool=arr.every(predicate,context);// returns truevarcount=context.count;// returns 3

BooleanArray.prototype.fill( value[, start[, end]] )

Returns a modified typed array filled with a fill value.

vararr=newBooleanArray(3);// Set all elements to the same value:arr.fill(true);varv=arr.get(0);// returns truev=arr.get(1);// returns truev=arr.get(2);// returns true// Fill all elements starting from the second element:arr.fill(false,1);v=arr.get(1);// returns falsev=arr.get(2);// returns false// Fill all elements from first element until the second-to-last element:arr.fill(false,0,2);v=arr.get(0);// returns falsev=arr.get(1);// returns false

When astart and/orend index is negative, the respective index is determined relative to the last array element.

vararr=newBooleanArray(3);// Set all array elements, except the last element, to the same value:arr.fill(true,0,-1);varv=arr.get(0);// returns truev=arr.get(2);// returns false

BooleanArray.prototype.filter( predicate[, thisArg] )

Returns a new array containing the elements of an array which pass a test implemented by a predicate function.

functionpredicate(v){return(v===true);}vararr=newBooleanArray(3);// Set the first three elements:arr.set(true,0);arr.set(false,1);arr.set(true,2);varout=arr.filter(predicate);// returns <BooleanArray>varlen=out.length;// returns 2varv=out.get(0);// returns truev=out.get(1);// return true

Thepredicate function is provided three arguments:

  • value: current array element.
  • index: current array element index.
  • arr: the array on which this method was called.

To set the function execution context, provide athisArg.

functionpredicate(v,i){this.count+=1;return(v===true);}vararr=newBooleanArray(3);varcontext={'count':0};arr.set(true,0);arr.set(false,1);arr.set(true,2);varout=arr.filter(predicate,context);// returns <BooleanArray>varlen=out.length;// returns 2varcount=context.count;// returns 3

BooleanArray.prototype.find( predicate[, thisArg] )

Returns the first element in an array for which a predicate function returns a truthy value.

functionpredicate(v){returnv===true;}vararr=newBooleanArray(3);arr.set(true,0);arr.set(false,1);arr.set(true,2);varv=arr.find(predicate);// returns true

Thepredicate function is provided three arguments:

  • value: current array element.
  • index: current array element index.
  • arr: the array on which this method was called.

To set the function execution context, provide athisArg.

functionpredicate(v){this.count+=1;return(v===true);}vararr=newBooleanArray(3);varcontext={'count':0};arr.set(false,0);arr.set(false,1);arr.set(true,2);varz=arr.find(predicate,context);// returns truevarcount=context.count;// returns 3

BooleanArray.prototype.findIndex( predicate[, thisArg] )

Returns the index of the first element in an array for which a predicate function returns a truthy value.

functionpredicate(v){returnv===true;}vararr=newBooleanArray(3);arr.set(true,0);arr.set(false,1);arr.set(true,2);varv=arr.findIndex(predicate);// returns 0

Thepredicate function is provided three arguments:

  • value: current array element.
  • index: current array element index.
  • arr: the array on which this method was called.

To set the function execution context, provide athisArg.

functionpredicate(v){this.count+=1;return(v===true);}vararr=newBooleanArray(3);varcontext={'count':0};arr.set(false,0);arr.set(false,1);arr.set(true,2);varz=arr.findIndex(predicate,context);// returns 2varcount=context.count;// returns 3

BooleanArray.prototype.findLast( predicate[, thisArg] )

Returns the last element in an array for which a predicate function returns a truthy value.

functionpredicate(v){returnv===true;}vararr=newBooleanArray(3);arr.set(true,0);arr.set(false,1);arr.set(true,2);varv=arr.findLast(predicate);// returns true

Thepredicate function is provided three arguments:

  • value: current array element.
  • index: current array element index.
  • arr: the array on which this method was called.

To set the function execution context, provide athisArg.

functionpredicate(v){this.count+=1;return(v===true);}vararr=newBooleanArray(3);varcontext={'count':0};arr.set(true,0);arr.set(false,1);arr.set(false,2);varz=arr.findLast(predicate,context);// returns truevarcount=context.count;// returns 3

BooleanArray.prototype.findLastIndex( predicate[, thisArg] )

Returns the index of the last element in an array for which a predicate function returns a truthy value.

functionpredicate(v){returnv===true;}vararr=newBooleanArray(3);arr.set(true,0);arr.set(false,1);arr.set(true,2);varv=arr.findLastIndex(predicate);// returns 2

Thepredicate function is provided three arguments:

  • value: current array element.
  • index: current array element index.
  • arr: the array on which this method was called.

To set the function execution context, provide athisArg.

functionpredicate(v){this.count+=1;return(v===true);}vararr=newBooleanArray(3);varcontext={'count':0};arr.set(true,0);arr.set(false,1);arr.set(false,2);varz=arr.findLastIndex(predicate,context);// returns 0varcount=context.count;// returns 3

BooleanArray.prototype.forEach( callbackFn[, thisArg] )

Invokes a function once for each array element.

functionlog(v,i){console.log('%s: %s',i.toString(),v.toString());}vararr=newBooleanArray(3);arr.set(true,0);arr.set(false,1);arr.set(true,2);arr.forEach(log);/* =>    0: true    1: false    2: true*/

The invoked function is provided three arguments:

  • value: current array element.
  • index: current array element index.
  • arr: the array on which this method was called.

To set the function execution context, provide athisArg.

functionfcn(v,i){this.count+=1;console.log('%s: %s',i.toString(),v.toString());}vararr=newBooleanArray(3);varcontext={'count':0};arr.set(true,0);arr.set(false,1);arr.set(true,2);arr.forEach(fcn,context);varcount=context.count;// returns 3

BooleanArray.prototype.get( i )

Returns an array element located at a nonnegative integer position (index)i.

vararr=newBooleanArray(10);// Set the first element:arr.set(true,0);// Get the first element:varv=arr.get(0);// returns true

If provided an out-of-bounds index, the method returnsundefined.

vararr=newBooleanArray(10);varv=arr.get(100);// returns undefined

BooleanArray.prototype.includes( searchElement[, fromIndex] )

Returns a boolean indicating whether an array includes a provided value.

vararr=newBooleanArray(5);arr.set(true,0);arr.set(false,1);arr.set(true,2);arr.set(true,3);arr.set(true,4);varbool=arr.includes(true);// returns truebool=arr.includes(false,2);// returns false

BooleanArray.prototype.indexOf( searchElement[, fromIndex] )

Returns the first index at which a given element can be found.

vararr=newBooleanArray(5);arr.set(true,0);arr.set(false,1);arr.set(true,2);arr.set(true,3);arr.set(true,4);varidx=arr.indexOf(true);// returns 0idx=arr.indexOf(false,1);// returns 1idx=arr.indexOf(true,-3);// returns 2

IfsearchElement is not present in the array, the method returns-1.

vararr=newBooleanArray(3);arr.set(true,0);arr.set(true,1);arr.set(true,2);varidx=arr.indexOf(false);// returns -1

BooleanArray.prototype.join( [separator] )

Returns a new string by concatenating all array elements.

vararr=newBooleanArray(3);arr.set(true,0);arr.set(false,1);arr.set(true,2);varstr=arr.join();// returns 'true,false,true'

By default, the method separates serialized array elements with a comma. To use an alternative separator, provide aseparator string.

vararr=newBooleanArray(3);arr.set(true,0);arr.set(false,1);arr.set(true,2);varstr=arr.join('|');// returns 'true|false|true'

BooleanArray.prototype.keys()

Returns an iterator for iterating over each index key in a typed array.

vararr=newBooleanArray(2);arr.set(true,0);arr.set(false,1);variter=arr.keys();varv=iter.next().value;// returns 0v=iter.next().value;// returns 1varbool=iter.next().done;// returns true

The returnediterator protocol-compliant object has the following properties:

  • next: function which returns aniterator protocol-compliant object containing the next iterated value (if one exists) assigned to avalue property and adone property having aboolean value indicating whether theiterator is finished.
  • return: function which closes aniterator and returns a single (optional) argument in aniterator protocol-compliant object.

BooleanArray.prototype.lastIndexOf( searchElement[, fromIndex] )

Returns the last index at which a given element can be found.

vararr=newBooleanArray(5);arr.set(true,0);arr.set(true,1);arr.set(true,2);arr.set(false,3);arr.set(true,4);varidx=arr.lastIndexOf(true);// returns 4idx=arr.lastIndexOf(false,3);// returns 3idx=arr.lastIndexOf(true,-3);// returns 2

IfsearchElement is not present in the array, the method returns-1.

vararr=newBooleanArray(3);arr.set(true,0);arr.set(true,1);arr.set(true,2);varidx=arr.lastIndexOf(false);// returns -1

BooleanArray.prototype.map( callbackFn[, thisArg] )

Returns a new array with each element being the result of a provided callback function.

functioninvert(v){return!v;}vararr=newBooleanArray(3);arr.set(true,0);arr.set(false,1);arr.set(true,2);varout=arr.map(invert);// returns <BooleanArray>varz=out.get(0);// returns falsez=out.get(1);// returns truez=out.get(2);// returns false

The callback function is provided three arguments:

  • value: current array element.
  • index: current array element index.
  • arr: the array on which this method was called.

To set the function execution context, provide athisArg.

functioninvert(v,i){this.count+=i;return!v;}vararr=newBooleanArray(3);varcontext={'count':0};arr.set(true,0);arr.set(false,1);arr.set(true,2);varout=arr.map(invert,context);// returns <BooleanArray>varcount=context.count;// returns 3;

BooleanArray.prototype.reduce( reducerFn[, initialValue] )

Applies a provided callback function to each element of the array, in order, passing in the return value from the calculation on the preceding element and returning the accumulated result upon completion.

functionreducer(acc,v){return(acc&&v);}vararr=newBooleanArray(3);arr.set(true,0);arr.set(false,1);arr.set(true,2);varout=arr.reduce(reducer);// returns false

The reducer function is provided four arguments:

  • acc: accumulated result.
  • value: current array element.
  • index: current array element index.
  • arr: the array on which this method was called.

By default, the function initializes the accumulated result to the first element in the array and passes the second array element asvalue during the first invocation of the provided callback. To begin accumulation from a different starting value and pass in the first array element asvalue during the first invocation of the provided callback, provide aninitialValue argument.

functionreducer(acc,v){if(v){returnacc+1;}returnacc;}vararr=newBooleanArray(3);arr.set(true,0);arr.set(false,1);arr.set(true,2);varout=arr.reduce(reducer,0);// returns 2

BooleanArray.prototype.reduceRight( reducerFn[, initialValue] )

Applies a provided callback function to each element of the array, in reverse order, passing in the return value from the calculation on the following element and returning the accumulated result upon completion.

functionreducer(acc,v){return(acc&&v);}vararr=newBooleanArray(3);arr.set(true,0);arr.set(false,1);arr.set(true,2);varout=arr.reduceRight(reducer);// returns false

The reducer function is provided four arguments:

  • acc: accumulated result.
  • value: current array element.
  • index: current array element index.
  • arr: the array on which this method was called.

By default, the function initializes the accumulated result to the last element in the array and passes the second-last array element asvalue during the first invocation of the provided callback. To begin accumulation from a different starting value and pass in the last array element asvalue during the first invocation of the provided callback, provide aninitialValue argument.

functionreducer(acc,v){if(v){returnacc+1;}returnacc;}vararr=newBooleanArray(3);arr.set(true,0);arr.set(false,1);arr.set(true,2);varout=arr.reduceRight(reducer,0);// returns 2

BooleanArray.prototype.reverse()

Reverses an array in-place.

vararr=newBooleanArray(3);arr.set(true,0);arr.set(false,1);arr.set(false,2);varout=arr.reverse();// returns <BooleanArray>varv=out.get(0);// returns falsev=out.get(1);// returns falsev=out.get(2);// returns true

BooleanArray.prototype.set( v[, i] )

Sets one or more array elements.

vararr=newBooleanArray(10);// Get the first element:varv=arr.get(0);// returns false// Set the first element:arr.set(true);// Get the first element:v=arr.get(0);// returns true

By default, the method sets array elements starting at position (index)i = 0. To set elements starting elsewhere in the array, provide an index argumenti.

vararr=newBooleanArray(10);// Get the fifth element:varv=arr.get(4);// returns false// Set the fifth element:arr.set(true,4);// Get the fifth element:v=arr.get(4);// returns true

In addition to providing a single value, to set one or more array elements, provide an array-like object containing truthy and falsy values

vararr=newBooleanArray(10);// Define an array of values:varbuf=['',1,null];// Set the fifth, sixth, and seventh elements:arr.set(buf,4);// Get the sixth element:varv=arr.get(5);// returns true

A few notes:

  • Ifi is out-of-bounds, the method throws an error.
  • If a target array cannot accommodate all values (i.e., the length of source array plusi exceeds the target array length), the method throws an error.
  • If provided atyped array which shares anArrayBuffer with the target array, the method will intelligently copy the source range to the destination range.

BooleanArray.prototype.slice( [start[, end]] )

Copies a portion of a typed array to a new typed array.

vararr=newBooleanArray(5);arr.set(true,0);arr.set(false,1);arr.set(true,2);arr.set(false,3);arr.set(true,4);varout=arr.slice();// returns <BooleanArray>varlen=out.length;// returns 5varbool=out.get(0);// returns truebool=out.get(len-1);// returns true

By default, the method returns a typed array beginning with the first array element. To specify an alternative array index at which to begin, provide astart index (inclusive).

vararr=newBooleanArray(5);arr.set(true,0);arr.set(false,1);arr.set(true,2);arr.set(false,3);arr.set(true,4);varout=arr.slice(1);// returns <BooleanArray>varlen=out.length;// returns 4varbool=out.get(0);// returns falsebool=out.get(len-1);// returns true

By default, the method returns a typed array which includes all array elements afterstart. To limit the number of array elements afterstart, provide anend index (exclusive).

vararr=newBooleanArray(5);arr.set(true,0);arr.set(false,1);arr.set(true,2);arr.set(false,3);arr.set(true,4);varout=arr.slice(1,-2);// returns <BooleanArray>varlen=out.length;// returns 2varbool=out.get(0);// returns falsebool=out.get(len-1);// returns true

BooleanArray.prototype.some( predicate[, thisArg] )

Returns a boolean indicating whether at least one element passes a test.

functionpredicate(v){returnv===true;}vararr=newBooleanArray(3);arr.set(false,0);arr.set(true,1);arr.set(false,2);varbool=arr.some(predicate);// returns true

Thepredicate function is provided three arguments:

  • value: current array element.
  • index: current array element index.
  • arr: the array on which this method was called.

To set the function execution context, provide athisArg.

functionpredicate(v){this.count+=1;returnv===true;}vararr=newBooleanArray(3);varcontext={'count':0};arr.set(false,0);arr.set(true,1);arr.set(false,2);varbool=arr.some(predicate,context);// returns truevarcount=context.count;// returns 2

BooleanArray.prototype.sort( [compareFcn] )

Sorts an array in-place.

functioncompare(a,b){if(a===false){if(b===false){return0;}return1;}if(b===true){return0;}return-1;}vararr=newBooleanArray(3);arr.set(true,0);arr.set(false,1);arr.set(true,2);arr.sort(compare);varv=arr.get(0);// returns truev=arr.get(1);// returns truev=arr.get(2);// returns false

ThecompareFcn determines the order of the elements. The function is called with the following arguments:

  • a: the first boolean value for comparison.
  • b: the second boolean value for comparison.

The function should return a number where:

  • a negative value indicates thata should come beforeb.
  • a positive value indicates thata should come afterb.
  • zero orNaN indicates thata andb are considered equal.

BooleanArray.prototype.subarray( [begin[, end]] )

Creates a new typed array view over the same underlyingArrayBuffer and with the same underlying data type as the host array.

vararr=newBooleanArray(5);arr.set(true,0);arr.set(false,1);arr.set(true,2);arr.set(false,3);arr.set(true,4);varsubarr=arr.subarray();// returns <BooleanArray>varlen=subarr.length;// returns 5varbool=subarr.get(0);// returns truebool=subarr.get(len-1);// returns true

By default, the method creates a typed array view beginning with the first array element. To specify an alternative array index at which to begin, provide abegin index (inclusive).

vararr=newBooleanArray(5);arr.set(true,0);arr.set(false,1);arr.set(true,2);arr.set(false,3);arr.set(true,4);varsubarr=arr.subarray(1);// returns <BooleanArray>varlen=subarr.length;// returns 4varbool=subarr.get(0);// returns falsebool=subarr.get(len-1);// returns true

By default, the method creates a typed array view which includes all array elements afterbegin. To limit the number of array elements afterbegin, provide anend index (exclusive).

vararr=newBooleanArray(5);arr.set(true,0);arr.set(false,1);arr.set(true,2);arr.set(false,3);arr.set(true,4);varsubarr=arr.subarray(1,-2);// returns <BooleanArray>varlen=subarr.length;// returns 2varbool=subarr.get(0);// returns falsebool=subarr.get(len-1);// returns true

BooleanArray.prototype.toLocaleString( [locales[, options]] )

Serializes an array as a locale-specific string.

vararr=newBooleanArray(3);arr.set(true,0);arr.set(false,1);arr.set(true,2);varstr=arr.toLocaleString();// returns 'true,false,true'

The method supports the following arguments:

  • locales: a string with a BCP 47 language tag or an array of such strings.
  • options: configuration properties.

BooleanArray.prototype.toReversed()

Returns a new typed array containing the elements in reversed order.

vararr=newBooleanArray(3);arr.set(true,0);arr.set(false,1);arr.set(false,2);varout=arr.toReversed();// returns <BooleanArray>varv=out.get(0);// returns falsev=out.get(1);// returns falsev=out.get(2);// returns true

BooleanArray.prototype.toSorted( [compareFcn] )

Returns a new typed array containing the elements in sorted order.

functioncompare(a,b){if(a===false){if(b===false){return0;}return1;}if(b===true){return0;}return-1;}vararr=newBooleanArray(3);arr.set(true,0);arr.set(false,1);arr.set(true,2);varout=arr.sort(compare);// returns <BooleanArray>varv=out.get(0);// returns truev=out.get(1);// returns truev=out.get(2);// returns false

ThecompareFcn determines the order of the elements. The function is called with the following arguments:

  • a: the first boolean value for comparison.
  • b: the second boolean value for comparison.

The function should return a number where:

  • a negative value indicates thata should come beforeb.
  • a positive value indicates thata should come afterb.
  • zero orNaN indicates thata andb are considered equal.

BooleanArray.prototype.toString()

Serializes an array as a string.

vararr=newBooleanArray(3);arr.set(true,0);arr.set(false,1);arr.set(true,2);varstr=arr.toString();// returns 'true,false,true'

BooleanArray.prototype.values()

Returns an iterator for iterating over each value in a typed array.

vararr=newBooleanArray(2);arr.set(true,0);arr.set(false,1);variter=arr.values();varv=iter.next().value;// returns truev=iter.next().value;// returns falsevarbool=iter.next().done;// returns true

The returnediterator protocol-compliant object has the following properties:

  • next: function which returns aniterator protocol-compliant object containing the next iterated value (if one exists) assigned to avalue property and adone property having aboolean value indicating whether theiterator is finished.
  • return: function which closes aniterator and returns a single (optional) argument in aniterator protocol-compliant object.

BooleanArray.prototype.with( index, value )

Returns a new typed array with the element at a provided index replaced with a provided value.

vararr=newBooleanArray(3);arr.set(true,0);arr.set(false,1);arr.set(true,1);varout=arr.with(0,false);// returns <BooleanArray>varv=out.get(0);// returns false

Notes

  • While aBooleanArraystrives to maintain (but does notguarantee) consistency withtyped arrays, significant deviations from ECMAScript-definedtyped array behavior are as follows:

    • The constructor doesnot require thenew operator.
    • Accessing array elements using bracket syntax (e.g.,X[i]) isnot supported. Instead, onemust use the.get() method.

Examples

varUint8Array=require('@stdlib/array-uint8');varlogEach=require('@stdlib/console-log-each');varBooleanArray=require('@stdlib/array-bool');// Create a boolean array by specifying a length:varout=newBooleanArray(3);logEach('%s',out);// Create a boolean array from an array of booleans:vararr=[true,false,true];out=newBooleanArray(arr);logEach('%s',out);// Create a boolean array from an array buffer:arr=newUint8Array([1,0,1,1,0,1]);out=newBooleanArray(arr.buffer);logEach('%s',out);// Create a boolean array from an array buffer view:arr=newUint8Array([1,0,1,1,0,1]);out=newBooleanArray(arr.buffer,1,2);logEach('%s',out);console.log('%s',false);

See Also


Notice

This package is part ofstdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.

For more information on the project, filing bug reports and feature requests, and guidance on how to developstdlib, see the main projectrepository.

Community

Chat


License

SeeLICENSE.

Copyright

Copyright © 2016-2025. The StdlibAuthors.


[8]ページ先頭

©2009-2025 Movatter.jp