简体   繁体   English

如何获取存储在对象数组中的总价值 - Javascript

[英]How to Get Total of Value stored in Object Array - Javascript

I'm trying to get the total price of values entered.我正在尝试获取输入值的总价。 It displays like this.它是这样显示的。

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

During the computation, different products should be computed separately and same product names should be computed as one - to be displayed in the the Like this.在计算过程中,不同的产品应分别计算,相同的产品名称应计算为一个——显示在Like this中。

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

I tried many times by calling or filtering, but it doesn't seem to work.我通过调用或过滤尝试了很多次,但似乎不起作用。 Not really sure now what I should do first.现在不太确定我应该先做什么。 Can you help me?你能帮助我吗?

Here is my html code这是我的 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>

And here is my Javascript.这是我的 Javascript。 I only have the code to add the product invetories.我只有添加产品库存的代码。 I still do not have the compute function yet.我还没有计算功能。 I delete my code as i am not sure now where to start.我删除了我的代码,因为我现在不确定从哪里开始。

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)
    }

this will be your js code这将是你的 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)
        }
    }

and your HTML code和你的 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>

First you need to convert the price value to integer because首先,您需要将价格值转换为整数,因为

document.getElement*("").value

gives you string, but you have to add the values.给你字符串,但你必须添加值。 In this case you nedd to convert the string to integer using在这种情况下,您需要使用以下命令将字符串转换为整数

parseInt(string)

or或者

Number(string)

or或者

parseFloat(string)

You just have to declare a "sum" variable and then add the values of arrayin for loop.您只需要声明一个“sum”变量,然后添加 arrayin for 循环的值。 This is how you can get the sum of all items这是你如何获得所有项目的总和

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

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