Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

js loop for of record (react/typescript)

I have several records{} with a lot of different data, where I need to get one which is object too. Can’t make two different for loop 🙁 anyone has an idea ?

my every record: RaRecord is an object

{sku: 1, name: 'TB', extended: {availability : YES, stock: 2} }
{sku: 2, name: 'EB', extended: {availability : No, stock: 4} }
{sku: 3, name: 'SB', extended: {availability : No, stock: 6} }

In the end I need to get an array of values of stock, like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

stock = [2, 4, 6]

>Solution :

Seems like your JSON has some issues but here is an example that should get you through

const data = [{sku: 1, name: 'TB', extended: {availability : 'YES', stock: 2} },
{sku: 1, name: 'TB', extended: {availability : 'NO', stock: 4} }]

const result = data.map(item => item.extended.stock);
console.log(result)


//if you want only available == yes
const result2 = data.filter(item => item.extended.availability === 'YES').map(item => item.extended.stock);
console.log(result2)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading