Object.keys和Object.values

##需求,数据格式是这样的,然后使用baidutemplate渲染页面的时候列数未知。##

###数据###
var data = {“state”:true,“data”:{“key”:“idd:ast:alldata”,“data”:[{“k0”:“2018-05-19”,“k1”:201970,“k2”:21732,“k3”:9.29,“k4”:7320,“k5”:244,“k6”:30.01,“k7”:194650,“k8”:21488,“k9”:9.06},{“k0”:“2018-05-18”,“k1”:191160,“k2”:20286,“k3”:9.42,“k4”:7240,“k5”:231,“k6”:31.34,“k7”:183920,“k8”:20055,“k9”:9.17},{“k0”:“2018-05-17”,“k1”:192805,“k2”:20452,“k3”:9.43,“k4”:6375,“k5”:268,“k6”:23.79,“k7”:186430,“k8”:20184,“k9”:9.24},{“k0”:“2018-05-16”,“k1”:191955,“k2”:20131,“k3”:9.54,“k4”:4765,“k5”:198,“k6”:24.07,“k7”:187190,“k8”:19933,“k9”:9.39},{“k0”:“2018-05-15”,“k1”:186710,“k2”:19245,“k3”:9.7,“k4”:3065,“k5”:188,“k6”:16.3,“k7”:183645,“k8”:19057,“k9”:9.64},{“k0”:“2018-05-14”,“k1”:46305,“k2”:5253,“k3”:8.81,“k4”:415,“k5”:23,“k6”:18.04,“k7”:45890,“k8”:5230,“k9”:8.77}],“wordbook”:{“k0”:"\u65e5\u671f",“k1”:"\u83b7\u53d6\u603b\u6570",“k2”:"\u83b7\u53d6\u603b\u4eba\u6570",“k3”:"\u4eba\u5747\u83b7\u53d6\u6570",“k4”:"\u6d88\u8017\u603b\u6570",“k5”:"\u6d88\u8017\u603b\u4eba\u6570",“k6”:"\u4eba\u5747\u6d88\u8017\u6570",“k7”:"\u672a\u6d88\u8017\u603b\u6570",“k8”:"\u672a\u6d88\u8017\u603b\u4eba\u6570",“k9”:"\u4eba\u5747\u672a\u6d88\u8017\u6570"}}};

###解决方案1:###
html代码:


	<%for(var item_title in wordbook){ %>
		<%=wordbook[item_title]%>
	<%}%>

####说明:####
使用for in 循环获取循环变量,渲染的时候根据循环变量获取值。
###解决方案2:###
html代码:

Object.keys(wordbook).length
Object.values(wordbook).length

####说明:####
使用Object.keys()或者Object.values()获取循环变量,渲染的时候根据循环变量获取值。Object.keys()函数返回索引(不仅仅是数字),Object.values()函数返回值。

新博客地址:http://www.caomage.com

你可能感兴趣的:(前端开发)