【问题标题】:How to Get Total of Value stored in Object Array - Javascript如何获取存储在对象数组中的值的总和 - Javascript
【发布时间】:2020-02-19 03:00:28
【问题描述】:

我正在尝试获取输入值的总价。它是这样显示的。

   Example:
   Name: Chair, Stock: 4, Price: 100
   Name: Chair, Stock: 3, Price: 50
   Name: Table, Stock: 4, Price: 100

在计算过程中,应分别计算不同的产品,并将相同的产品名称计算为一个 - 以显示在 Like this 中。

chars in total: 550
table in total: 400
Total inventory price: 950

我通过调用或过滤尝试了很多次,但似乎不起作用。现在不太确定我应该先做什么。 你能帮帮我吗?

这是我的html代码

    <div>
        <h3>Part 2</h3>
        <form id="products" onsubmit="inventory(event)" action="#">
            <table>
                <tr>
                    <td>Name</td>
                    <td>: <input type="text" id="productName" required></td>
                </tr>
                <tr>
                    <td>Stocks</td>
                    <td>: <input type="text" id="productStock" required></td>
                </tr>
                <tr>
                    <td>Price</td>
                    <td>: <input type="text" id="prodPrice" required></td>
                </tr>
            </table>
            <input type="Submit" value="Add">
        </form>
        <h4>Product List</h4>
        <div id="inventoryList"></div>
        <input type="button" onclick="compute(event)" value="Calculate for local value for each product">
        <h4>Product Total Value</h4>
        <div id="inventorytotal"></div>
    </div>

这是我的 Javascript。我只有添加产品库存的代码。我还没有计算功能。我删除了我的代码,因为我现在不确定从哪里开始。

var prodInventory = []; 
function inventory(e){
    e.preventDefault();

    var products = {
        prodName: document.getElementById('productName').value,
        prodStock: document.getElementById('productStock').value,
        prodPrice: document.getElementById('prodPrice').value
    }

    prodInventory.push(products);
    var myJSON = JSON. stringify(products);
    document.forms[0].reset();

    //Display of Inventory List
    document.getElementById("inventoryList").innerHTML = "";
    for (let x in prodInventory) {
        document.getElementById("inventoryList").innerHTML += "name: " + prodInventory[x].prodName + " , " + "stocks : " +  prodInventory[x].prodStock + " , " + "price: " + prodInventory[x].prodPrice + "<br>";
        console.log(prodInventory)
    }

【问题讨论】:

    标签: javascript html arrays object sum


    【解决方案1】:

    这将是你的 js 代码

    var prodInventory = []; 
        function inventory(){
    
            var products = {
                prodName: document.getElementById('productName').value,
                prodStock: document.getElementById('productStock').value,
                prodPrice: parseInt(document.getElementById('prodPrice').value)
            }
    
            prodInventory.push(products);
            var myJSON = JSON. stringify(products);
            document.forms[0].reset();
    
            //Display of Inventory List
            document.getElementById("inventoryList").innerHTML = "";
            for (let x in prodInventory) {
                document.getElementById("inventoryList").innerHTML += "name: " + prodInventory[x].prodName + " , " + "stocks : " +  prodInventory[x].prodStock + " , " + "price: " + prodInventory[x].prodPrice + "<br>";
                console.log(prodInventory)
            }
    
            return false;
        }
    
        function compute() {
            document.getElementById("inventorytotal").innerHTML = "";
            var sum = 0;
            for (let x in prodInventory) {
                sum += prodInventory[x].prodPrice;
                document.getElementById("inventorytotal").innerHTML = sum;
                console.log(prodInventory)
            }
        }
    

    和你的 HTML 代码

    <div>
                                <h3>Part 2</h3>
                                <form id="products" onsubmit="inventory()" action="#">
                                    <table>
                                        <tr>
                                            <td>Name</td>
                                            <td>: <input type="text" id="productName" required></td>
                                        </tr>
                                        <tr>
                                            <td>Stocks</td>
                                            <td>: <input type="text" id="productStock" required></td>
                                        </tr>
                                        <tr>
                                            <td>Price</td>
                                            <td>: <input type="text" id="prodPrice" required></td>
                                        </tr>
                                    </table>
                                    <input type="Submit" value="Add">
                                </form>
                                <h4>Product List</h4>
                                <div id="inventoryList"></div>
                                <input type="button" onclick="compute()"
                                    value="Calculate for local value for each product">
                                <h4>Product Total Value</h4>
                                <div id="inventorytotal"></div>
                            </div>
    

    首先您需要将价格值转换为整数,因为

    document.getElement*("").value
    

    为您提供字符串,但您必须添加值。在这种情况下,您需要使用

    将字符串转换为整数
    parseInt(string)
    

    Number(string)
    

    parseFloat(string)
    

    您只需要声明一个“sum”变量,然后添加arrayin for 循环的值。这样你就可以得到所有项目的总和

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-02
      • 1970-01-01
      • 2021-09-02
      • 2022-01-02
      • 2020-02-10
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多