-
-
Notifications
You must be signed in to change notification settings - Fork 907
feat: add array/base/pluck
#5376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 2 commits
cfab7bf
172380f
9b01b13
67b6a3a
2acd65b
42ad68c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
<!-- | ||
|
||
@license Apache-2.0 | ||
|
||
Copyright (c) 2025 The Stdlib Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
||
--> | ||
|
||
# pluck | ||
|
||
> Extract a property value from elements in an input object array and assign results to elements in a new output array. | ||
|
||
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||
|
||
<section class="intro"> | ||
|
||
</section> | ||
|
||
<!-- /.intro --> | ||
|
||
<!-- Package usage documentation. --> | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var pluck = require( '@stdlib/array/base/pluck' ); | ||
``` | ||
|
||
#### pluck( x, prop ) | ||
|
||
Extracts a property value from elements in an input object array and assigns results to elements in a new output array. | ||
|
||
```javascript | ||
var x = [ | ||
{ | ||
'a': 1 | ||
}, | ||
{ | ||
'a': 2 | ||
}, | ||
{ | ||
'a': 3 | ||
} | ||
]; | ||
|
||
var y = pluck( x, 'a' ); | ||
// returns [ 1, 2, 3 ] | ||
``` | ||
|
||
The function accepts the following arguments: | ||
|
||
- **x**: input array. | ||
- **prop**: property to extract values from. | ||
|
||
#### pluck.assign( x, prop, out, stride, offset ) | ||
|
||
Extracts a property value from elements in an input object array and assigns results to elements in an output array. | ||
|
||
```javascript | ||
var x = [ | ||
{ | ||
'a': 1 | ||
}, | ||
{ | ||
'a': 2 | ||
}, | ||
{ | ||
'a': 3 | ||
} | ||
]; | ||
|
||
var out = [ 0, 0, 0 ]; | ||
var y = pluck.assign( x, 'a', out, 1, 0 ); | ||
// returns [ 1, 2, 3 ] | ||
``` | ||
|
||
The function accepts the following arguments: | ||
|
||
- **x**: input array. | ||
- **prop**: property to extract values from. | ||
headlessNode marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
- **out**: output array. | ||
- **stride**: stride length for output array. | ||
- **offset**: starting index for output array. | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="notes"> | ||
|
||
## Notes | ||
|
||
- The function skips `undefined` and `null` array elements. | ||
|
||
<!-- eslint-disable object-property-newline, object-curly-newline, object-curly-spacing --> | ||
|
||
```javascript | ||
var arr = [ | ||
{ 'a': 1, 'b': 2 }, | ||
null, | ||
void 0, | ||
{ 'a': 0.5, 'b': 3 } | ||
]; | ||
|
||
var out = pluck( arr, 'a' ); | ||
// returns [ 1, , , 0.5 ] | ||
``` | ||
|
||
- Extracted values are **not** cloned. | ||
|
||
<!-- eslint-disable object-property-newline, object-curly-newline, object-curly-spacing --> | ||
|
||
```javascript | ||
var arr = [ | ||
{ 'a': { 'b': 2 } }, | ||
{ 'a': { 'b': 3 } } | ||
]; | ||
|
||
var out = pluck( arr, 'a' ); | ||
// returns [ { 'b': 2 }, { 'b': 3 } ] | ||
|
||
var bool = ( arr[ 0 ].a === out[ 0 ] ); | ||
// returns true | ||
``` | ||
|
||
- The function supports array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]). | ||
|
||
|
||
</section> | ||
|
||
<!-- /.notes --> | ||
|
||
<!-- Package usage examples. --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
var randu = require( '@stdlib/random/base/randu' ); | ||
var round = require( '@stdlib/math/base/special/round' ); | ||
var pluck = require( '@stdlib/array/base/pluck' ); | ||
|
||
var arr; | ||
var tmp; | ||
var out; | ||
var i; | ||
var j; | ||
|
||
// Generate a 100x5 2d-array... | ||
arr = new Array( 100 ); | ||
for ( i = 0; i < arr.length; i++ ) { | ||
tmp = new Array( 5 ); | ||
for ( j = 0; j < tmp.length; j++ ) { | ||
tmp[ j ] = round( randu()*100.0*(j+1.0) ); | ||
} | ||
arr[ i ] = tmp; | ||
} | ||
|
||
// Pluck the 3rd column: | ||
out = pluck( arr, 2 ); | ||
console.log( out ); | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.examples --> | ||
|
||
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="references"> | ||
|
||
</section> | ||
|
||
<!-- /.references --> | ||
|
||
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
||
<section class="related"> | ||
|
||
</section> | ||
|
||
<!-- /.related --> | ||
|
||
<!-- Section for all links. Do not manually edit this section, as it is automatically populated. --> | ||
|
||
<section class="links"> | ||
|
||
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// MODULES // | ||
|
||
var bench = require( '@stdlib/bench' ); | ||
var isArray = require( '@stdlib/assert/is-array' ); | ||
var randu = require( '@stdlib/random/base/randu' ); | ||
var round = require( '@stdlib/math/base/special/round' ); | ||
var pkg = require( './../package.json' ).name; | ||
var pluck = require( './../lib' ); | ||
|
||
|
||
// MAIN // | ||
|
||
bench( pkg+':assign', function benchmark( b ) { | ||
var out; | ||
var tmp; | ||
var x; | ||
var y; | ||
var i; | ||
|
||
headlessNode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
x = new Array( 100 ); | ||
for ( i = 0; i < x.length; i++ ) { | ||
tmp = new Array( 5 ); | ||
for ( j = 0; j < tmp.length; j++ ) { | ||
Check failure on line 43 in lib/node_modules/@stdlib/array/base/pluck/benchmark/benchmark.assign.js
|
||
tmp[ j ] = round( randu()*100.0*(j+1.0) ); | ||
Check failure on line 44 in lib/node_modules/@stdlib/array/base/pluck/benchmark/benchmark.assign.js
|
||
} | ||
x[ i ] = tmp; | ||
} | ||
out = new Array( x.length ); | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
x[ i % x.length ][ i % 5 ] = randu(); | ||
out = pluck.assign( x, 2, y, 1, 0 ); | ||
if ( typeof out !== 'object' ) { | ||
b.fail( 'should return an array' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( !isArray( out ) ) { | ||
b.fail( 'should return an array' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// MODULES // | ||
|
||
var bench = require( '@stdlib/bench' ); | ||
var isArray = require( '@stdlib/assert/is-array' ); | ||
var randu = require( '@stdlib/random/base/randu' ); | ||
var round = require( '@stdlib/math/base/special/round' ); | ||
var pkg = require( './../package.json' ).name; | ||
var pluck = require( './../lib' ); | ||
|
||
|
||
// MAIN // | ||
|
||
bench( pkg, function benchmark( b ) { | ||
var out; | ||
var tmp; | ||
var x; | ||
var i; | ||
var j; | ||
|
||
x = new Array( 100 ); | ||
for ( i = 0; i < x.length; i++ ) { | ||
tmp = new Array( 5 ); | ||
for ( j = 0; j < tmp.length; j++ ) { | ||
tmp[ j ] = round( randu()*100.0*(j+1.0) ); | ||
} | ||
x[ i ] = tmp; | ||
} | ||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
x[ i % x.length ][ i % 5 ] = randu(); | ||
out = pluck( x, 2 ); | ||
if ( typeof out !== 'object' ) { | ||
b.fail( 'should return an array' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( !isArray( out ) ) { | ||
b.fail( 'should return an array' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); |
Uh oh!
There was an error while loading. Please reload this page.