I am constructing a JSON object that is an array of products. Using a select box, I want to find the object in the array that has an ID that matches the value from the selectedIndex of the select box and return the price. Here is the JSON script: var products = [{''ID'': 16378, ''Cost'': 100.00},{''ID'': 16377,''Cost'': 112.00},{''ID'': 16376,''Cost'': 141.00}] I am getting the ID value from the select box with this: var id = $F("mySelectBox"); How would I use prototype to find the object with the matching ID and return it''s cost? Thanks! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
i think you use find with a predicate/iterator function (i prefer to call this one a predicate) var id = $F("mySelectBox"); var selectedItem = products.find(function(item){ return (item.ID == id); }); alert(selectedItem.inspect()); On Jan 18, 2008 11:36 AM, KilLrBuNieZ <eightseventeen-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I am constructing a JSON object that is an array of products. Using a > select box, I want to find the object in the array that has an ID that > matches the value from the selectedIndex of the select box and return > the price. > > Here is the JSON script: > > var products = [{''ID'': 16378, ''Cost'': 100.00},{''ID'': 16377,''Cost'': > 112.00},{''ID'': 16376,''Cost'': 141.00}] > > I am getting the ID value from the select box with this: > > var id = $F("mySelectBox"); > > How would I use prototype to find the object with the matching ID and > return it''s cost? > > Thanks! > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
I tried your example, which is similar to what I was attempting: var id = $F("mySelectBox"); var selectedItem = products.find(function(item){ return (item.ID =id); }); alert(selectedItem.inspect()); ...and the I got an exception on the alert line: "selectedItem has no properties" I know the id exists, so what could be the problem? This should be a lot simpler than it is turning out to be. On Jan 17, 4:42 pm, "Gareth Evans" <agr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> i think you use find with a predicate/iterator function (i prefer to call > this one a predicate) > > var id = $F("mySelectBox"); > var selectedItem = products.find(function(item){ return (item.ID == id); }); > alert(selectedItem.inspect()); > > On Jan 18, 2008 11:36 AM, KilLrBuNieZ <eightsevent...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I am constructing a JSON object that is an array of products. Using a > > select box, I want to find the object in the array that has an ID that > > matches the value from the selectedIndex of the select box and return > > the price. > > > Here is the JSON script: > > > var products = [{''ID'': 16378, ''Cost'': 100.00},{''ID'': 16377,''Cost'': > > 112.00},{''ID'': 16376,''Cost'': 141.00}] > > > I am getting the ID value from the select box with this: > > > var id = $F("mySelectBox"); > > > How would I use prototype to find the object with the matching ID and > > return it''s cost? > > > Thanks!--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
KilLrBuNieZ wrote:> I tried your example, which is similar to what I was attempting: > > var id = $F("mySelectBox"); > var selectedItem = products.find(function(item){ return (item.ID => id); }); > alert(selectedItem.inspect()); > > ...and the I got an exception on the alert line: "selectedItem has no > properties" >the selectedItem is an object, particularly {''ID'': 16377,''Cost'': 112.00} when id is 16377, for example. Try alert(selectedItem.Cost); but would it be easier to store the info right on the checkbox in the first place? <input type="checkbox" name="product[16378]" value="100.00" /> <input type="checkbox" name="product[16377]" value="112.00" /> <input type="checkbox" name="product[16376]" value="141.00" /> $("mySelectBox").value; - Ken Snyder --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Sorry my mistake. I forgot you can''t use inspect on objects. I should have used Object.toJSON(selectedItem) It was there for debugging though, to access specific properties use selectedItem.<propertynamecasesensitive> without the angle brackets. On Jan 18, 2008 12:41 PM, Ken Snyder <kendsnyder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > KilLrBuNieZ wrote: > > I tried your example, which is similar to what I was attempting: > > > > var id = $F("mySelectBox"); > > var selectedItem = products.find(function(item){ return (item.ID => > id); }); > > alert(selectedItem.inspect()); > > > > ...and the I got an exception on the alert line: "selectedItem has no > > properties" > > > the selectedItem is an object, particularly {''ID'': 16377,''Cost'': 112.00} > when id is 16377, for example. Try > > alert(selectedItem.Cost); > > > but would it be easier to store the info right on the checkbox in the > first place? > > <input type="checkbox" name="product[16378]" value="100.00" /> > <input type="checkbox" name="product[16377]" value="112.00" /> > <input type="checkbox" name="product[16376]" value="141.00" /> > > $("mySelectBox").value; > > - Ken Snyder > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---