Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I change var in jQuery(JavaScript)?
    primarykey
    data
    text
    <p>After an AJAX call, I get the JSON data like this:</p> <pre><code>{ "detailPrice": { "server": { "amount": "8", "usedPrice": "10", "discountPrice": "-1", "totalPrice": "9" }, "network": { "amount": "8", "usedPrice": "10", "discountPrice": "-1", "totalPrice": "9" }, "storage": { "amount": "8", "usedPrice": "10", "discountPrice": "-1", "totalPrice": "9" }, "loadBalancer": { "amount": "8", "usedPrice": "10", "discountPrice": "-1", "totalPrice": "9" }, "others": { "amount": "8", "usedPrice": "10", "discountPrice": "-1", "totalPrice": "9" }, "support": { "amount": "8", "usedPrice": "10", "discountPrice": "-1", "totalPrice": "9" }, "totalPrice": { "totalUsedPrice": "8", "totalDiscountPrice": "-2", "missedPrice": "10", "tax": "9", "otherDiscount": "-1" } } } </code></pre> <p>I have to append these data to the view, so I made a code like this:</p> <pre><code>var serverpriceHTML = "총 " + result.detailPrice.server.amount + "대&lt;br/&gt;"; serverpriceHTML += "이용요금 " + result.detailPrice.server.usedPrice + "원&lt;br/&gt;"; serverpriceHTML += "할인요금 " + result.detailPrice.server.discountPrice + "원&lt;br/&gt;"; var serverTotalPriceHTML = result.detailPrice.server.totalPrice + "원"; $("#server_price").html(serverpriceHTML); $("#server_totalprice").html(serverTotalPriceHTML); var networkpriceHTML = "총 " + result.detailPrice.network.amount + "대&lt;br/&gt;"; networkpriceHTML += "이용요금 " + result.detailPrice.network.usedPrice + "원&lt;br/&gt;"; networkpriceHTML += "할인요금 " + result.detailPrice.network.discountPrice + "원&lt;br/&gt;"; var networkTotalPriceHTML = result.detailPrice.network.totalPrice + "원"; $("#network_price").html(networkpriceHTML); $("#network_totalprice").html(networkTotalPriceHTML); </code></pre> <p>As you can see, it has a lot of duplicated code, so I'm trying to make a private function about it.</p> <p>The problem is:</p> <pre><code>result.detailPrice.server.amount </code></pre> <p>I have to change <code>server</code> to <code>network</code>, <code>storage</code>, <code>loadbalancer</code> and etc. to get the data, but I'm not sure how can I change this.</p> <p>If I make like:</p> <pre><code>function makeHTML(price, totalPrice, name) { var test = "result.detailPrice" + name + ".amount"; var serverpriceHTML = "총 " + test + "대&lt;br/&gt;"; serverpriceHTML += "이용요금 " + result.detailPrice.server.usedPrice + "원&lt;br/&gt;"; serverpriceHTML += "할인요금 " + result.detailPrice.server.discountPrice + "원&lt;br/&gt;"; var serverTotalPriceHTML = result.detailPrice.server.totalPrice + "원"; $(price).html(serverpriceHTML); $(totalPrice).html(serverTotalPriceHTML); } </code></pre> <p>This is just adding string test. Any good idea about this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload