简体   繁体   English

在JavaScript中添加数组项

[英]Add items of array in javascript

I want to add price of all items in form of total from below code. 我想从以下代码中以总计形式添加所有items price

document.getElementById('form-signin').addEventListener('submit', saveItem);
function saveItem(e){
   var items= document.getElementById('items').value;
   var date= document.getElementById('date').value;
   var price= document.getElementById('price').value;
   var expense= {
    items: items,
    date: date,
    price: price
}


if(localStorage.getItem('bill')==null){
var bill=[];
 bill.push(expense);
 localStorage.setItem('bill', JSON.stringify(bill));


} else{

    var bill = JSON.parse(localStorage.getItem('bill'));
    bill.push(expense);
    localStorage.setItem('bill', JSON.stringify(bill));
    console.log(bill);

    }
e.preventDefault();
} 

function fetchResult(){
  var bill = JSON.parse(localStorage.getItem('bill'));
  var result = document.getElementById('total');


   result.innerHTML='';
   for(var i=0;i < bill.length;i++){
   var items= bill[i].items;
   var date= bill[i].date;
   var price= bill[i].price;




  result.innerHTML+= '<table class="table table-bordered">' + '<tr>'+
  '<th>'+'Items'+'</th>'+
  '<th>'+'Date'+'</th>'+
  '<th>'+'Price'+'</th>'+
   '</tr>'+ '<tr>'+
   '<td>'+ items + '</td>'+
   '<td>'+ date+ '</td>'+
   '<td>'+ price + '</td>'+
   '</tr>'+ '</table>';

}

This is output of aboce. 这是boce的输出。 Now how to calculate total ?? 现在如何计算总计?

Eduardo's sample will work, but I went a little further if you're interested in another way to accomplish what you're doing. Eduardo的示例将起作用,但是如果您对完成您正在做的事情的另一种方法感兴趣,我会走得更远。 I made some changes to help you prevent duplicating column headers and clean up the table generation a bit. 我进行了一些更改,以帮助您防止重复的列标题并稍微清理表生成。 This code does require jQuery however, and the table needs to already exist. 这段代码确实需要jQuery,并且该表必须已经存在。

The form is just there because I needed something to test against... you can remove that part. 该表格就在那儿,因为我需要进行测试...您可以删除该部分。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>

<form>
  Items: <input type="text" id="items"><br>
  Date: <input type="text" id="date"><br>
  Price: <input type="text" id="price"><br>
  <input type="submit" value="Submit" onClick="saveItem()" >
</form> 

<table id="tbl-items" class="table table-bordered"><thead><tr><th>Items</th><th>Date</th><th>Price</th></tr></thead><tbody id="tbl-items-body"></tbody></table>
<script>
    document.getElementById('form-signin').addEventListener('submit', saveItem);

    function saveItem(){
        var bill=[];

        var expense= { // didn't need the variables.
            items: document.getElementById('items').value,
            date: document.getElementById('date').value,
            price: document.getElementById('price').value
        }

        if(localStorage.getItem('bill') != null){
            bill = JSON.parse(localStorage.getItem('bill'));
        }

        bill.push(expense);
        localStorage.setItem('bill', JSON.stringify(bill))
        console.log(bill);
    } 

    function fetchResult(){
        var bill = JSON.parse(localStorage.getItem('bill'));
        var totalItems = 0;
        var totalPrice = 0;

        for(var i=0;i < bill.length;i++){
        totalItems++;
        totalPrice += bill[i].price;

            $("#tbl-items-body").append(
                $('<tr>').append(
                    '<td>' + bill[i].items + '</td><td>' + bill[i].date + '</td><td>' + bill[i].price + '</td>'
                )
            );
        }

        $("#tbl-items-body").append(
            $('<tr>').append(
                '<td>Total Items: ' + totalItems + '</td><td>Total Price: ' + totalPrice + '</td>'
            )
        );
    }
</script>

In your fetchResult function while you are adding the price values and displaying in table, add to price to a total variable, then display. 在添加价格值并显示在表格中的fetchResult函数中,将价格加到总变量中,然后显示。

function fetchResult(){
...

   result.innerHTML='';

   var total = 0;

   for(var i=0;i < bill.length;i++){
   var items= bill[i].items;
   var date= bill[i].date;
   var price= bill[i].price;

   total += price;



  result.innerHTML+= '<table class="table table-bordered">' + '<tr>'+
  '<th>'+'Items'+'</th>'+
....

    <span> total </span>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM