Last active
June 2, 2023 10:13
-
-
Save ruslan-avantis/2bc8fd96160b9a4fdabddcbbc512c503 to your computer and use it in GitHub Desktop.
Example of working with arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Promise = require('bluebird') | |
const items = { | |
employers: [ | |
{ | |
name: 'qqqq', | |
cost: 1111, | |
}, | |
{ | |
name: 'weferf', | |
cost: 2222, | |
} | |
] | |
} | |
// Promise.map | |
const getOne = async employers => { | |
let i = 0 | |
return await Promise.map(employers, employer => { | |
const resp = { | |
i, | |
properties: employer | |
} | |
i++ | |
return resp | |
}, { concurrency: 1 }) | |
} | |
// Will return a new array | |
console.log('getOne: ', getOne(items.employers)) | |
// Promise.filter | |
const filterEmployers = async (employers, filter) => { | |
return await Promise.filter(employers, employer => employer.cost === filter) | |
} | |
// Will return the filtered array | |
console.log('res2: ', getDuo(items.employers, 1111)) | |
// array find | |
const findOne = (employers, find) => employers.find(employer => employer.cost === find) | |
// return object or undefined | |
console.log('findOne: ', findOne(items.employers, 1111)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment