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 on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!
Take elements from either one of two arrays depending on a condition.
To use in Observable,
where = require( 'https://cdn.jsdelivr.net/gh/stdlib-js/array-base-where@umd/browser.js' )
To vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:
var where = require( 'path/to/vendor/umd/array-base-where/index.js' )
To include the bundle in a webpage,
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/stdlib-js/array-base-where@umd/browser.js"></script>
If no recognized module system is present, access bundle contents via the global scope:
<script type="text/javascript">
(function () {
window.where;
})();
</script>
Takes elements from either x
or y
depending on a condition.
var x = [ 1, 2, 3, 4 ];
var y = [ 5, 6, 7, 8 ];
var condition = [ true, false, true, false ];
var z = where( condition, x, y );
// returns [ 1, 6, 3, 8 ]
The function supports the following parameters:
- condition: array of values indicating whether to take an element from either
x
ory
. If a condition element is truthy, the function takes a respective element fromx
; otherwise, the function takes a respective element fromy
. If non-empty, must be broadcast compatible with the resolved output array length. - x: first input array. If
condition
is non-empty, must be broadcast compatible with the resolved output array length. - y: second input array. If
condition
is non-empty, must be broadcast compatible with the resolved output array length.
When all input arrays are non-empty, the function supports broadcasting single-element arrays to the resolved output array length, which is equal to the maximum length of all provided input arrays.
var x = [ 1, 2, 3, 4 ];
var y = [ 5 ];
var condition = [ true, false, true, false ];
var z = where( condition, x, y );
// returns [ 1, 5, 3, 5 ]
z = where( condition, y, x );
// returns [ 5, 2, 5, 4 ]
z = where( [ true ], x, y );
// returns [ 1, 2, 3, 4 ]
z = where( [ false ], x, y );
// returns [ 5, 5, 5, 5 ]
z = where( condition, [ 1 ], y );
// returns [ 1, 5, 1, 5 ]
If condition
is an empty array, the function returns an empty array.
var x = [ 1, 2, 3, 4 ];
var y = [ 5, 6, 7, 8 ];
var condition = [];
var z = where( condition, x, y );
// returns []
Takes elements from either x
or y
depending on a condition and assigns the values to elements in a provided output array.
var x = [ 1, 2, 3, 4 ];
var y = [ 5, 6, 7, 8 ];
var out = [ 0, 0, 0, 0 ];
var condition = [ true, false, true, false ];
var arr = where.assign( condition, x, y, out, 1, 0 );
// returns [ 1, 6, 3, 8 ]
var bool = ( arr === out );
// returns true
The function supports the following parameters:
- condition: array of values indicating whether to take an element from either
x
ory
. If a condition element is truthy, the function takes a respective element fromx
; otherwise, the function takes a respective element fromy
. If non-empty, must be broadcast compatible with the output array. - x: first input array. If
condition
is non-empty, must be broadcast compatible with the output array. - y: second input array. If
condition
is non-empty, must be broadcast compatible with the output array. - out: output array.
- stride: output array stride.
- offset: output array offset.
The function supports broadcasting single-element arrays to the output array length.
var x = [ 1, 2, 3, 4 ];
var y = [ 5 ];
var condition = [ true, false, true, false ];
var out = [ 0, 0, 0, 0 ];
var arr = where.assign( condition, x, y, out, 1, 0 );
// returns [ 1, 5, 3, 5 ]
out = [ 0, 0, 0, 0 ];
arr = where.assign( condition, y, x, out, 1, 0 );
// returns [ 5, 2, 5, 4 ]
out = [ 0, 0, 0, 0 ];
arr = where.assign( [ true ], x, y, out, 1, 0 );
// returns [ 1, 2, 3, 4 ]
out = [ 0, 0, 0, 0 ];
arr = where.assign( [ false ], x, y, out, 1, 0 );
// returns [ 5, 5, 5, 5 ]
out = [ 0, 0, 0, 0 ];
arr = where.assign( condition, [ 1 ], y, out, 1, 0 );
// returns [ 1, 5, 1, 5 ]
When condition
is an empty array, the function returns the output array unchanged.
var x = [ 1, 2, 3, 4 ];
var y = [ 5, 6, 7, 8 ];
var out = [ 0, 0, 0, 0 ];
var condition = [];
var arr = where.assign( condition, x, y, out, 1, 0 );
// returns [ 0, 0, 0, 0 ]
var bool = ( arr === out );
// returns true
<!DOCTYPE html>
<html lang="en">
<body>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/stdlib-js/random-array-discrete-uniform@umd/browser.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/stdlib-js/random-array-bernoulli@umd/browser.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/stdlib-js/array-base-where@umd/browser.js"></script>
<script type="text/javascript">
(function () {
var opts = {
'dtype': 'generic'
};
// Generate an array of indicator values:
var condition = bernoulli( 20, 0.9, opts );
console.log( condition );
// Generate an array of random values:
var x = discreteUniform( condition.length, 0, 10, opts );
console.log( x );
// Define an array containing a broadcasted "missing" value:
var y = [ NaN ];
// Return an array with randomly placed missing values:
var z = where( condition, x, y );
console.log( z );
})();
</script>
</body>
</html>
This package is part of stdlib, 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 develop stdlib, see the main project repository.
See LICENSE.
Copyright © 2016-2025. The Stdlib Authors.