从键值对中获取json值而不在meteor中传递键

时间:2015-06-16 09:56:00

标签: javascript json mongodb meteor

我正在使用键来获取流星中json的值。我的json就是这种格式。

    {
"_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>

我得到的结果。但问题是json不是固定格式。有时候它会包含更少的值和更多的时间。我不想在HTML中硬编码密钥。有没有其他方法可以得到这个。我不想硬编码像{{this.Material.value}},{{this.Price.value}},.......等等。

2 个答案:

答案 0 :(得分:2)

<强> 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;

  }

});

您可以使用此

来避免所有for循环
var thingsToDisplay = [];

for (var i = itemsArray.length - 1; i >= 0; i--) {

  var singleItem = displayOrder.map(function(keyName){
    return itemsArray[i][keyName];
  });

  thingsToDisplay.push(singleItem);

};

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>

答案 1 :(得分:0)

下面的内容应该会有所帮助:

<table>
  <thead>
    <tr>
     {{#each headers}}
       <th>{{this}}</th>
     {{/each}}
    </tr>
  </thead>
  ...

模板:

{{1}}