Quoting through an array using ColdFusion

I have an array of shopping carts that has a variable to let me know if the product is an accessory or not, it will be either yes or no. I need to scroll through the cart and find out the following:

  • If the basket contains only accessories; do anything.
  • If the basket is just products; do anything.
  • If the basket contains goods and accessories; do anything.

I tried this:

<cfloop index="i" from="1" to="#arrayLen(session.mycart)#"> <cfif session.mycart[i].accs EQ "yes"> <cfset accPresent = "yes"> </cfif> <cfif session.mycart[i].accs EQ "no"> <cfset prodpresent = "yes"> </cfif> </cfloop> <cfif accPresent EQ "yes" and prodPresent EQ "no"> <cfset bothPresent EQ "yes"> </cfif> 

This crashes because accPresent was not found, which I think is due to the fact that the loop goes one at a time, and accs is not equal to yes when it finds a non-accessory product. What is the best way to achieve what I'm trying to do?

+6
source share
4 answers

Do it

 <cfset accPresent = "no" /> <cfset prodPresent = "no" /> <cfloop index="i" from="1" to="#arrayLen(session.mycart)#"> <cfif session.mycart[i].accs EQ "yes"> <cfset accPresent = "yes"> </cfif> <cfif session.mycart[i].accs EQ "no"> <cfset prodpresent = "yes"> </cfif> </cfloop> <cfif accPresent EQ "yes" and prodPresent EQ "no"> <cfset bothPresent EQ "yes"> </cfif> 
+6
source

Jason

In its third statement, it is assumed that AccPresent and ProdPresent will exist. Did you create them first and specify default values? Try the following:

 <cfparam name="accPresent" default="no"/> <cfparam name="prodPresent" default="no"/> <cfloop index="i" from="1" to="#arrayLen(session.mycart)#"> <cfif session.mycart[i].accs EQ "yes"> <cfset accPresent = "yes"> </cfif> <cfif session.mycart[i].accs EQ "no"> <cfset prodpresent = "yes"> </cfif> </cfloop> <cfif accPresent EQ "yes" and prodPresent EQ "no"> <cfset bothPresent EQ "yes"> </cfif> 

This assumes that each of them should be set to no by default.

+3
source

In CF 10 (or Railo 4) this can be done more elegantly using cfscript and the Underscore.cfc library :

 _ = new Underscore(); myCart = duplicate(session.myCart); accPresent = _.any(myCart, function(val) { return val.accs; }); prodPresent = _.any(myCart, function(val) { return !val.accs; }); bothPresent = accPresent && prodPresent; 

The great thing about _.find () is that it stops as soon as the iterator function returns true, so you don't have to iterate over each element in the array.

Note. Using duplcate () is recommended when accessing shared-area variables to prevent deadlocks.

(Disclaimer: I wrote Underscore.cfc)

+1
source

In versions of ColdFusion 8 and above, a <cfloop> can use the array directly

 <cfloop index="i" array="#session.mycart#"> <cfif i.accs EQ "yes"> <cfset accPresent = "yes"> </cfif> <cfif i.accs EQ "no"> <cfset prodpresent = "yes"> </cfif> </cfloop> <cfif accPresent EQ "yes" and prodPresent EQ "no"> <cfset bothPresent EQ "yes"> </cfif> 

Note. This i refers to the structure containing the data, not the position of the data

http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_j-l_15.html

+1
source

Source: https://habr.com/ru/post/908279/


All Articles