Extract values from objects that meet a specific condition and add those values to a new array
![pachocastillosr](https://us.v-cdn.net/6037124/uploads/userpics/MC7ELS8NG1HC/nPLP98RNMYYEZ.jpg)
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!
Best Answer
-
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
-
Hi,
Are you taking those variables from xano queries or from some external source?
-
That helped me solve it @Ray Deck ! Thank you and thank you too @Pawel Magdanski !
-
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!
-
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!
Categories
- All Categories
- 53 ? Announcements
- 47 ? Releases
- 37 ? Welcome
- 983 ? Help! I'm a Noob
- 125 ? No-Code Front-Ends
- 633 ? Working with APIs
- 439 ? Transforming data
- 126 ? Connect Xano to ...
- 50 ?? Find an Expert
- 348 ❓Other questions
- 35 ? Security
- 22 ✂️ Snippets
- 19 ? Showcase
- 7 ?️ Xano Chatter
- 62 ? Video Tutorials
- 171 ? Request a feature
- 229 ? Report a Bug
- 19 ? Templates & Extensions
- 7 ? Feedback