Click here to Skip to main content
14,385,566 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have this array
using javascript ,i want to iterate this array

[
{"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 13-Mar-12 0:30am
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

read this[^].
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

Assuming JQuery, heres an example with html.
The interesting stuff happens on the function inside the $.each

<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>
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Here is a short example of how to read the objects from the array:

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
   
v3
Rate this:
Please Sign up or sign in to vote.

Solution 4

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!
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100