
Need help looping through a javascript object (nested loops)
- or -
Post a project like this3011
$$
- Posted:
- Proposals: 2
- Remote
- #1408553
- Awarded
Description
Experience Level: Intermediate
General information for the website: Not a website, but a javascript object
Description of requirements/features: I ideally want a loop that can go through the 2 subobjects named j and k in digitalDataCart.CartItems and give me back each value for adults, children and code for each "FoodPerRoom".
I need to loop through the different subobjects and get back the values for each subobject in a string.
for(j=0; j<10; j++) for(k=0; k<10; k++)
digitalDatacart.CartItems[2].productInfo.FoodPerRoom["j"].codes["k"].adults
digitalDatacart.CartItems[2].productInfo.FoodPerRoom["j"].codes["k"].children
digitalDataCart.CartItems[2].productInfo.FoodPerRoom["j"].codes["k"].code
j can be anything from 0 to 15
k can be anything from 0 to 15
I need a javascript loop function that can go through the three different object and give me back the given values in one string. I ideally don't want to output any value that is equal to 0 in the "adults" and "children" objects. so there needs to be a condition that looks for the value, and if the value is 0 for adults or children, then don't output it, even if there is a "code" value.
The ideal output would be:
1 x 11 // which is "adults x code"
Extra notes: Once approved I will be able to send through a copy of the javascript variable as a sample, which will make it easier to test the loops function.
Just need to add something extra to this task that might complicate things a bit further.
The "code" = digitalDataCart.CartItems[2].productInfo.FoodPerRoom["j"].codes["k"].code can be the same across different subobjects and I need to ensure that if a code is present in two or three different subobjects, that I get the number of adults or children summed up, so it is displayed in one string.
The javascript function is being created for a breakfast selection. So each breakfast has a unique code. But the way data is displayed is that it is showing breakfast codes per room. Since multiple rooms can have the same breakfast code, I need to sum of the number of adults that have the same type of breakfast code, if the value is "0", then we don't output that.
Ideally the output would look like this (if a user has 4 adult breakfast and 3 children breakfast:
var food = 4 x 11, 3 x 15 //4 x adult breakfast, 3 x children breakfast
Description of requirements/features: I ideally want a loop that can go through the 2 subobjects named j and k in digitalDataCart.CartItems and give me back each value for adults, children and code for each "FoodPerRoom".
I need to loop through the different subobjects and get back the values for each subobject in a string.
for(j=0; j<10; j++) for(k=0; k<10; k++)
digitalDatacart.CartItems[2].productInfo.FoodPerRoom["j"].codes["k"].adults
digitalDatacart.CartItems[2].productInfo.FoodPerRoom["j"].codes["k"].children
digitalDataCart.CartItems[2].productInfo.FoodPerRoom["j"].codes["k"].code
j can be anything from 0 to 15
k can be anything from 0 to 15
I need a javascript loop function that can go through the three different object and give me back the given values in one string. I ideally don't want to output any value that is equal to 0 in the "adults" and "children" objects. so there needs to be a condition that looks for the value, and if the value is 0 for adults or children, then don't output it, even if there is a "code" value.
The ideal output would be:
1 x 11 // which is "adults x code"
Extra notes: Once approved I will be able to send through a copy of the javascript variable as a sample, which will make it easier to test the loops function.
Just need to add something extra to this task that might complicate things a bit further.
The "code" = digitalDataCart.CartItems[2].productInfo.FoodPerRoom["j"].codes["k"].code can be the same across different subobjects and I need to ensure that if a code is present in two or three different subobjects, that I get the number of adults or children summed up, so it is displayed in one string.
The javascript function is being created for a breakfast selection. So each breakfast has a unique code. But the way data is displayed is that it is showing breakfast codes per room. Since multiple rooms can have the same breakfast code, I need to sum of the number of adults that have the same type of breakfast code, if the value is "0", then we don't output that.
Ideally the output would look like this (if a user has 4 adult breakfast and 3 children breakfast:
var food = 4 x 11, 3 x 15 //4 x adult breakfast, 3 x children breakfast

Waqas R.
100% (11)Projects Completed
15
Freelancers worked with
10
Projects awarded
37%
Last project
15 Jan 2019
United Kingdom
New Proposal
Login to your account and send a proposal now to get this project.
Log inClarification Board Ask a Question
-
Hi Waqas,
Could you by any chance paste the JavaScript variable into a jsFiddle (www.jsfiddle.com) for me to take a look at?
Thanks,
ChrisWaqas R.04 Jan 2017Hi Chris, I have posted it here: https://jsfiddle.net/waqasrafiq/md59afsr/1/
StreamSoft Development Ltd04 Jan 2017Like this? https://jsfiddle.net/md59afsr/2/ :)
-
Please purchase my hourlie,I will fix it.
https://www.peopleperhour.com/hourlie/fix-any-php-html-css-javascript-mysql-wordpress-psd-html-yii-cakephp-codeigniter-xml/221878
278932278920
We collect cookies to enable the proper functioning and security of our website, and to enhance your experience. By clicking on 'Accept All Cookies', you consent to the use of these cookies. You can change your 'Cookies Settings' at any time. For more information, please read ourCookie Policy
Cookie Settings
Accept All Cookies