Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have this array
using javascript ,i want to iterate this array

Java
[
{"Operand":"ofItems","Value":"100","Operator":"=","FilterCondition":"ofItems = 100"},
{"Operand":"OrderTotal","Value":"$250","Operator":"=","FilterCondition":"OrderTotal = $250"},
{"Operand":"WebShipMethod","Value":"car","Operator":"=","FilterCondition":"WebShipMethod = car"}
]
after i have to get the each details from this array
How to do this
Posted
Updated 12-Mar-12 23:30pm
v2

read this[^].
 
Share this answer
 
Assuming JQuery, heres an example with html.
The interesting stuff happens on the function inside the $.each

XML
<html><head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">

$(document).ready(function () {

    var items = [ {"Operand":"ofItems","Value":"100","Operator":"=","FilterCondition":"ofItems = 100"},
                {"Operand":"OrderTotal","Value":"$250","Operator":"=","FilterCondition":"OrderTotal = $250"},
                  {"Operand":"WebShipMethod","Value":"car","Operator":"=","FilterCondition":"WebShipMethod = car"}];

    var trTemplate = "<tr><td>@Operand</td><td>@Value</td><td>@Operator</td><td>@FilterCondition</td></tr>";
    $.each(items, function(ix,itm){
         $("#tblOutput tbody:last").append(
            trTemplate
            .replace("@Operand",itm.Operand)
            .replace("@Value", itm.Value)
            .replace("@Operator",itm.Operator)
            .replace("@FilterCondition", itm.FilterCondition)
         );

    });

});


</script>
</head>

<body>
<table id="tblOutput" border="1px">
<thead><tr><th style='width:20px'>Operand</th><th>Value</th><th>Operator</th><th>FilterCondition</th></tr></thead>
<tbody>
</tbody>
</table>
</body>
</html>
 
Share this answer
 
Here is a short example of how to read the objects from the array:

JavaScript
var items = [ {"Operand":"ofItems","Value":"100","Operator":"=","FilterCondition":"ofItems = 100"},
              {"Operand":"OrderTotal","Value":"$250","Operator":"=","FilterCondition":"OrderTotal = $250"},
              {"Operand":"WebShipMethod","Value":"car","Operator":"=","FilterCondition":"WebShipMethod = car"}];

for(var idx = 0; idx < items.length; idx++)
{
    // Here we get an element from the array
    var element = items[idx];

    // And here we access the properties of the objects
    var operand = element.Operand;
    var value   = element.Value;
    var operator= element.Operator;
    
    // Here would be a convenient place to do stuff with the values we read from the object
    // TODO: Do stuff
}


A great resource for web related base technologies please visit W3CSchool[^]s they have a nice section on Javascript[^] which you should find very informative.

Regards,

Manfred
 
Share this answer
 
v3
proper way in jquery is:
var x = [{"Operand":"ofItems","Value":"100","Operator":"=","FilterCondition":"ofItems = 100"},
{"Operand":"OrderTotal","Value":"$250","Operator":"=","FilterCondition":"OrderTotal = $250"},
{"Operand":"WebShipMethod","Value":"car","Operator":"=","FilterCondition":"WebShipMethod = car"}];

$.each(x, function(ind, val){
     alert( x[ind].Operand );
     alert( x[ind].Value );
     alert( x[ind].FilterCondition );
});


how's that!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900