Javascript 从键值对获取json值,而无需在meteor中传递键
我使用键从meteor中的json获取值。我的json就是这种格式Javascript 从键值对获取json值,而无需在meteor中传递键,javascript,json,mongodb,meteor,Javascript,Json,Mongodb,Meteor,我使用键从meteor中的json获取值。我的json就是这种格式 { "_id" : "SXTJBs7QLXoyMFGpK", "Brand" : { "value" : "Nike" }, "Material" : { "value" : "Cooton" }, "Price" : { "value" : "67484" }, "ComboId" : { "value" : "y23" }, "Color" : { "value" : "Whi
{
"_id" : "SXTJBs7QLXoyMFGpK",
"Brand" : {
"value" : "Nike"
},
"Material" : {
"value" : "Cooton"
},
"Price" : {
"value" : "67484"
},
"ComboId" : {
"value" : "y23"
},
"Color" : {
"value" : "White"
},
"LaunchDate" : {
"value" : "08/02/2015"
},
"DiscountActiveDate" : {
"value" : "08/03/2015"
},
"DiscountInactiveDate" : {
"value" : "08/04/2015"
},
"Category" : {
"value" : "Sport"
},
"ProductSubCategory" : {
"value" : "trackpant"
},
"Status" : "Pending",
"TemplateID" : {
"value" : "557fc7d06ecb48d38a67a380"
}
所以对于值,我使用这样的键
<tbody>
{{#each product}}
<tr>
<td>{{this.Brand.value}}</td>
<td>{{this.Material.value}}</td>
<td>{{this.Price.value}}</td>
<td>{{this.ComboId.value}}</td>
<td>{{this.Color.value}}</td>
<td>{{this.LaunchDate.value}}</td>
<td>{{this.DiscountActiveDate.value}}</td>
<td>{{this.DiscountInactiveDate.value}}</td>
<td>{{this.Category.value}}</td>
<td>{{this.ProductSubCategory.value}}</td>
</tr>
{{/each}}
</tbody>
{{{#每种产品}
{{this.Brand.value}
{{this.Material.value}
{{this.Price.value}
{{this.ComboId.value}
{{this.Color.value}
{{this.LaunchDate.value}
{{this.discountactivate.value}
{{this.discountingActiveDate.value}
{{this.Category.value}
{{this.ProductSubCategory.value}
{{/每个}}
我正在得到结果。但问题是json不是固定格式。有时它会包含较少的值,有时会包含较多的值。我不想在HTML中硬编码键。还有别的办法吗。我不想硬编码像{{this.Material.value}}、{{{this.Price.value}}、…..这样的值。下面这样的内容应该会有帮助:
Template.NAME.helpers({
headers:function(){
// assumption : parsed JSON is in this.json
var json = this.json;
// get all keys from object json
keys = _.keys(json)
// assumption : 'Status', 'TemplateID', '_id' are never a header
// remove keys which shouldn't be treated as header
keys = _.without(keys, 'Status', 'TemplateID', '_id')
return keys;
}
})
模板:
<table>
<thead>
<tr>
{{#each headers}}
<th>{{this}}</th>
{{/each}}
</tr>
</thead>
...
{{{#每个头}}
{{this}}
{{/每个}}
...
JS
Template.test.helpers({
itemsArray: function(){
// This would normally be the result of your Mongo query
var itemsArray = [
{
"_id": "SXTJBs7QLXoyMFGpK",
"Brand": "Nike",
"Material": "Cooton",
"Price": "67484",
"ComboId": "y23",
"Color": "White",
"LaunchDate": "08/02/2015",
"DiscountActiveDate": "08/03/2015",
"DiscountInactiveDate": "08/04/2015",
"Category": "Sport",
"ProductSubCategory": "trackpant",
"Status": "Pending",
"TemplateID": "557fc7d06ecb48d38a67a380"
}, {
"_id": "IGJHihljiUYG6787y",
"Brand": "Adidas",
"Material": "Polyamide",
"Color": "Silver",
"Status": "Pending",
"TemplateID": "557fc7d06ecb48d38a67a380"
}
];
// specify the order you want things to be displayed
var displayOrder = [
"_id",
"Brand",
"Material",
"Price",
"ComboId",
"Color",
"LaunchDate",
"DiscountActiveDate",
"DiscountInactiveDate",
"Category",
"ProductSubCategory",
"Status",
"TemplateID"
];
// You want to end up create an array like this and
// sending it to a template helper
// var thingsToDisplay = [
// [
// "SXTJBs7QLXoyMFGpK",
// "Nike",
// "Cooton",
// "67484"
// "y23",
// "White",
// "08/02/2015",
// "08/03/2015",
// "08/04/2015",
// "Sport",
// "trackpant",
// "Pending",
// "557fc7d06ecb48d38a67a380"
// ],[
// "IGJHihljiUYG6787y",
// "Adidas",
// "Polyamide",
// "",
// "",
// "",
// "",
// "",
// "",
// "",
// "",
// "Pending",
// "557fc7d06ecb48d38a67a380"
// ]
// ];
// thingsToDisplay will have the same length as itemsArray
// initialize thingsToDisplay
var thingsToDisplay = [];
for(var j = 0; j < itemsArray.length; j++){
thingsToDisplay[j] = [];
for(var i = 0; i < displayOrder.length; i++){
if( itemsArray[j][ displayOrder[i] ] ){
thingsToDisplay[j][i] = itemsArray[j][ displayOrder[i] ];
} else {
thingsToDisplay[j][i] = "";
};
};
};
console.log("The finalized thingsToDisplay array: ", thingsToDisplay);
return thingsToDisplay;
}
});
HTML
<template name="test">
<h1>I used Bootstrap 3</h1>
<table class="table">
<thead>
<tr>
<th>_id</th>
<th>Brand</th>
<th>Material</th>
<th>Price</th>
<th>ComboId</th>
<th>Color</th>
<th>LaunchDate</th>
<th>DiscountActiveDate</th>
<th>DiscountInactiveDate</th>
<th>Category</th>
<th>ProductSubCategory</th>
<th>Status</th>
<th>TemplateID</th>
</tr>
</thead>
<tbody>
{{#each itemsArray}}
<tr>
{{#each this}}
<td>{{this}}</td>
{{/each}}
</tr>
{{/each}}
</tbody>
</table>
</template>
我用了bootstrap3
_身份证
烙印
材料
价格
组合ID
颜色
发射日期
打折
折扣有效期
类别
产品子类别
地位
模板体
{{{#每个项目都有}
{{{#每个这个}
{{this}}
{{/每个}}
{{/每个}}
兄弟,我想要的是价值观而不是关键。这意味着我不明白你的目标是什么。请把你的问题写得更准确些。我不想硬编码像{{{this.Material.value}},{{{{this.Price.value}},},…..这样的值。但是你希望这些值在页面上仍然以这种顺序出现吗?品牌、材料、价格、ComboId等?为什么每个关键字名称后都有值?您只需执行{“Color”:“White”}
<template name="test">
<h1>I used Bootstrap 3</h1>
<table class="table">
<thead>
<tr>
<th>_id</th>
<th>Brand</th>
<th>Material</th>
<th>Price</th>
<th>ComboId</th>
<th>Color</th>
<th>LaunchDate</th>
<th>DiscountActiveDate</th>
<th>DiscountInactiveDate</th>
<th>Category</th>
<th>ProductSubCategory</th>
<th>Status</th>
<th>TemplateID</th>
</tr>
</thead>
<tbody>
{{#each itemsArray}}
<tr>
{{#each this}}
<td>{{this}}</td>
{{/each}}
</tr>
{{/each}}
</tbody>
</table>
</template>