Search code examples
reactjsfor-loopobject

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: {status : YES, element: 2} }
{sku: 2, name: 'EB', extended: {status : No, element: 4} }
{sku: 3, name: 'SB', extended: {status : No, element: 6} }

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

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)