Extract values from objects that meet a specific condition and add those values to a new array

Options
pachocastillosr
pachocastillosr Member
edited April 2023 in ? Help! I'm a Noob

Hi,

I have 2 variables. One variable is "products", which is an array of products and the other variable is "product_ids", which an array of product IDs.

If I wanted to create a new array "prices" that contains the prices of all the products whose id is in the "product_ids" array, how could I do this?

Example:

products =
[
{
id: 1,
name: 'Product 1',
price: 9.99,
description: 'Description of product 1'
},

{
id: 2,
name: 'Product 2',
price: 19.99,
description: 'Description of product 2'
}
]



product_ids = [1, 3]

Since "1" is in the "products_ids" array, then "9.99" (the price of Product 1) would be added to the "prices" array.


Any help would be appreciated, thanks!

Tagged:

Best Answer

  • Ray Deck
    Ray Deck Trusted Xano Expert ✭✭✭
    edited April 2023 Answer ✓
    Options

    This is a filter + mapping problem for your array. There are three ways to do it that I can think of, but I would probably start with:

    1) Create variable a blank array and call it "prices"
    2) Make a for-each loop for your products array
    3) Run an "exists in array" data manipulation function checking whether item.id is in product_ids (via $this === item.id)
    3) Make an IF test asking if the outcome of (3) is true
    4) "then" add item.price to the end of "prices."

    At the end of the loop, you will have at the end the two prices in an array!

    We work on data manipulation problems like this frequently as part of our "hardest 5%" in the Statechange forums and daily office hours. (https://statechange.ai)

Answers

  • Pawel Magdanski
    Pawel Magdanski Member ✭✭✭
    Options

    Hi,

    Are you taking those variables from xano queries or from some external source?

  • pachocastillosr
    Options

    That helped me solve it @Ray Deck ! Thank you and thank you too @Pawel Magdanski !

  • sachith
    sachith Member
    Options

    i have 2 variables user_id and jobs.

    var user_id = 1;

    var Jobs =

    [

    {

    id: 1,

    name: ‘job 1',

    description: 'Description of job 1’,

    applied_users =[

    {

      user_id: 1,

      user_img: url

    },{

      user_id: 3,

      user_img: url

    },{

      user_id: 4,

      user_img: url

    }

    ]

    },

    {

    id: 2,

    name: 'Product 2',

    description: 'Description of job 2’,

    applied_users =[

    {

      user_id: 1,

      user_img: url

    },{

      user_id: 5,

      user_img: url

    }

    ]

    }

    ]

    jobs variable, coming from table called jobs. so i want to update the user_img of user_id is equal to 1 in every applied_user array.

    Any help would be appreciated, thanks!

  • sachith
    sachith Member
    Options

    i have 2 variables user_id and jobs.

    var user_id = 1;

    var Jobs =

    [

    {

    id: 1,

    name: ‘job 1',

    description: 'Description of job 1’,

    applied_users =[

    {

      user_id: 1,

      user_img: url

    },{

      user_id: 3,

      user_img: url

    },{

      user_id: 4,

      user_img: url

    }

    ]

    },

    {

    id: 2,

    name: 'Product 2',

    description: 'Description of job 2’,

    applied_users =[

    {

      user_id: 1,

      user_img: url

    },{

      user_id: 5,

      user_img: url

    }

    ]

    }

    ]

    jobs variable, coming from table called jobs. so i want to update the user_img of user_id is equal to 1 in every applied_user array.

    Any help would be appreciated, thanks!