For loop(JavaScript)..迭代绑定中保存的值..?

时间:2012-10-18 21:32:30

标签: javascript

我正在尝试迭代

中的值
  • oldData.results.bindings.date
  • oldData.results.bindings.liability
  • oldData.results.bindings.stock
  • oldData.results.bindings.event
  • oldData.results.bindings.current

...包含所有值。我想记录它们(尽管我是'等于'或'小于'oldData.results.bindings.length),并输出,例如:

document.write(simpleHead[0] +
  '{Date: "' +
  oldData.results.bindings[0].date +
  '", Liability: ' +
  oldData.results.bindings[0].liability +
  ", Stock: " +
  oldData.results.bindings[0].stock +
  ", " +
  oldData.results.bindings[0].event +
  ": " +
  oldData.results.bindings[0].current +
  ",}" +
  simpleTail[0]);

目前,我只使用'... [0] ...'(如上面的脚本中所示)打印其中一个数据集,但是我会喜欢使用为每个数据集写入'... [i] ...'的循环。


当前代码:

...
function httpGet(theUrl)
{
var xmlHttp = null;
var xmlHttp = null;
xmlHttp = new XMLHttpRequest();         // instantiate request
xmlHttp.open( "GET", theUrl, false );   // open url
xmlHttp.send( null );                   // sending nothing
return xmlHttp.responseText;            // return url's data as text
};

var result = httpGet("#local#");        // assign this url to the variable name "result"

function revive (key, value) {
    if (value.datatype == "http://www.w3.org/2001/XMLSchema#double" ||  // if datatype is this
    value.datatype == "http://www.w3.org/2001/XMLSchema#integer" ||     // or, this
    value.datatype == "http://www.w3.org/2001/XMLSchema#float")         // or, this
    {
        return (parseInt(value.value))      // if '#double', '#integer', or '#schema', then: 'vars' label + convert the datatype's float value to integer 
    }
    else if (value.type == 'literal')
    {
        return (value.value)        // if datatype's value is a literal: 'vars' label + return as a string
    }
    else if (value.datatype == 'http://www.w3.org/2001/XMLSchema#date') 
    {
        return value.value      // if "XMLSchema#date's" value is a literal: 'vars' label + return as a string
    }
    else 
    {
        return value        // if datatype is anything else: 'vars' label + return value as a string
    }
};
var scriptHead = ["YUI().use('charts',function(Y){var myDataValues=<br /><br />["];
var scriptTail = ["<br />];<br /><br />var styleDef={series:{Stock:{line:{color:'#EEB647'},marker:{fill:{color:'#eee',alpha:0,wmode:'transparent'},border:{color:'#222',alpha:0,wmode:'transparent'},over:{fill:{color:'#eee'},border:{color:'#000'},width:9,height:9}}},Liability:{line:{color:'#171944'},marker:{fill:{color:'#eee',alpha:0,wmode:'transparent'},border:{color:'#222',alpha:0,wmode:'transparent'},over:{fill:{color:'#eee'},border:{color:'#000'},width:9,height:9}}},Shipment:{line:{color:'#ff0000',alpha:0,wmode:'transparent'},marker:{fill:{color:'#eee',alpha:0,wmode:'transparent'},border:{color:'#ff0000',alpha:0,wmode:'transparent'},over:{fill:{color:'#ff0000',alpha:0,wmode:'transparent'},border:{color:'#000',alpha:0,wmode:'transparent'},width:16,height:16}}},Production:{line:{color:'#FFD700',alpha:0,wmode:'transparent'},marker:{fill:{color:'#eee',alpha:0,wmode:'transparent'},border:{color:'#FFD700',alpha:0,wmode:'transparent'},over:{fill:{color:'#FFD700',alpha:0,wmode:'transparent'},border:{color:'#000',alpha:0,wmode:'transparent'},width:16,height:16}}},Order:{line:{color:'#006400',alpha:0,wmode:'transparent'},marker:{fill:{color:'#eee',alpha:0,wmode:'transparent'},border:{color:'#006400',alpha:0,wmode:'transparent'},over:{fill:{color:'#006400',alpha:0,wmode:'transparent'},border:{color:'#000',alpha:0,wmode:'transparent'},width:16,height:16}}}}};var myAxes={dateRange:{keys:['date'],position:'bottom',type:'category',title:'Date Range',styles:{majorTicks:{display:'none'},label:{rotation:-45,margin:{top:5}},title:{fontSize:'90%'}}}};var mychart=new Y.Chart({dataProvider:myDataValues,interactionType:'planar',render:'#mychart',categoryKey:'Date',styles:styleDef,categoryType:'time',horizontalGridlines:{styles:{line:{color:'#fff'}}},verticalGridlines:{styles:{line:{color:'#fff'}}}})});<br /><br />"];
var simpleHead = [scriptHead];
var simpleTail = [scriptTail];
var oldData = JSON.parse(result, revive);
</script>
</head>
<body>
<p>
<script language="JavaScript" type="text/javascript">
    // variable? or something here? 

    for(i=0; i<oldData.results.bindings.length ;i++)
    {
        document.write(simpleHead[0] + '<br />{Date: "' + oldData.results.bindings[i].date + '", Liability: ' + oldData.results.bindings[i].liability + ", Stock: " + oldData.results.bindings[i].stock + ", " + oldData.results.bindings[i].event + ": " + oldData.results.bindings[i].current + ",}" + simpleTail[0] + "<br /><br />");
    }
</script>
</p>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您已经在使用循环,特别是包含索引数组查找的for循环。

例如:

oldData.results.bindings[i].date