简体   繁体   English

如何计算和添加 object 中键的值

[英]How to count and add the values of keys in a object

I need to write a function that takes in 2 arguments, the first argument is a list of <li> elements and the second argument is an object.我需要编写一个 function,它接收 2 个 arguments,第一个参数是<li>元素的列表,第二个参数是 object。 The object is list of products and their prices. object 是产品列表及其价格。 All the <li> elements will have text containing a name of a product.The function should check the price of each product mentioned, calculate and return the total price.所有<li>元素都将包含包含产品名称的文本。function 应检查提到的每个产品的价格,计算并返回总价格。

Example:例子:

<ul>
    <li>oreos<li/>
    <li>milk<li/>
<ul/>
let products = {
    oreos: 20,
    milk:  17,
    bread: 12,
    doritos: 10
}

I have tried using a for in/of loop to go through the products object and match it to the <li> using .innerText but am unsure of how to go about doing it.我尝试通过产品 object 使用 for in/of 循环到 go 并使用 .innerText 将其与<li>匹配,但不确定如何使用.innerText进行操作。

Please help out请帮忙

What you can do is that, you can use the following function:您可以做的是,您可以使用以下 function:

let totalPrice = (priceList, products) => {
return products.reduce((total, elem) => {
    total += priceList[elem.innerText];
    return total;
}, 0)

} }

This function iterates on list of products using Array.reduce and adds the list of product prices and returns the total.此 function 使用 Array.reduce 迭代产品列表并添加产品价格列表并返回总计。

You should checkout Array.reduce() for more clarity.您应该检查Array.reduce()以获得更清晰的信息。

Loop through the li elements, use their innerText as the key to lookup the price, sum them, and you're good:遍历 li 元素,使用它们的 innerText 作为键来查找价格,将它们相加,你就很好了:

let total = 0;
document.querySelectorAll('li').forEach(item => {
  total += products[item.innerText];
});

There are other forms of this, and little additions you could include (such as checking that the item actually exists in the products array first), but that's the basic idea.还有其他 forms ,您可以添加一些小东西(例如首先检查该项目是否确实存在于 products 数组中),但这是基本思想。

Here's what your function could look like:这是您的 function 的样子:

  1. Iterate over all list items遍历所有列表项
  2. Get the text content of the item获取item的文本内容
  3. look up the price in the products object by using the text content as key以文字内容为关键字,在产品object中查找价格
  4. add to the total加到总数中
  5. return the total返回总计

I used Array.from() and Array.prototype.reduce() to achieve this all in one go.我使用Array.from()Array.prototype.reduce()在 go 中实现了这一切。 Of course you'd need a distinct class or even better an ID on the list instead of simply selecting all list items on the page.当然,您需要一个独特的 class 甚至更好的列表上的 ID,而不是简单地选择页面上的所有列表项。

 let products = { oreos: 20, milk: 17, bread: 12, doritos: 10 } function getTotal(listElements, productPrices) { var total = Array.from(listElements).reduce(function(total, listItem) { total += productPrices[listItem.textContent] || 0; return total; }, 0); return total; } console.log(getTotal(document.querySelectorAll('li'), products));
 <ul> <li>oreos</li> <li>milk</li> </ul>

You can add an id, let's say "list" to your <ul> , then do something like this:您可以添加一个 id,让我们说“列表”到您的<ul> ,然后执行以下操作:

let list = document.getElementById("list");
let totals = {};

for(let key of products) {
    totals[key] = 0;
}

for(let i = 0; i < list.children.length; i++) {
    let child = list.children[i];

    let text = child.textContent;

    if(totals.hasOwnProperty(text)) {
        totals[text]++;
    }
}

let fullPrice = 0;

for(let key of products) {
    fullPrice += products[key] * totals[key];
}

console.log(fullPrice);

The code does the following:该代码执行以下操作:

  1. It creates a totals object, which will count the occurrences of each existing product in your products object.它会创建一个totals object,它将计算您的products object 中每个现有产品的出现次数。
  2. Then, it gets your <ul> element, and loops through its <li> children elements, taking the text of each <li> and looking it up in the totals object.然后,它获取您的<ul>元素,并遍历其<li>子元素,获取每个<li>的文本并在totals object 中查找它。
  3. If the product exists, it increases the count.如果产品存在,它会增加计数。
  4. Lastly, it calculates the full, final price by multiplying the totals with the prices.最后,它通过将总价乘以价格来计算完整的最终价格。

PS: This code assumes there will be only <li> elements inside your list, and that each <li> holds only text. PS:此代码假定列表中只有<li>元素,并且每个<li>仅包含文本。

You can try with querySelectorAll() and Array.prototype.reduce() .您可以尝试使用querySelectorAll()Array.prototype.reduce()

Please Note: You are closing the elements in the wrong way ( <li/>, <ul/> ) which will create unexpected markup, should be </li>, </ul> .请注意:您以错误的方式关闭元素( <li/>, <ul/> ),这将创建意外标记,应该是</li>, </ul>

 let products = { oreos: 20, milk: 17, bread: 12, doritos: 10 }; function getTotal(li, prod){ return Array.from(li).reduce((a,c) => a + prod[c.textContent], 0); } console.log(getTotal(document.querySelectorAll('li'), products));
 <ul> <li>oreos</li> <li>milk</li> </ul>

You fist need to fix your end-tags;你首先需要修复你的结束标签; the slash comes before the final tag name.斜杠出现在最终标签名称之前。

Second, you can reduce all your list values by grabbing their text value and looking them up in the product list.其次,您可以通过获取它们的文本值并在产品列表中查找它们来减少所有列表值。 You can use Array.from to translate a NodeList into an array so that you can call Array.prototype.reduce .您可以使用Array.fromNodeList转换为数组,以便可以调用Array.prototype.reduce

 const products = { oreos: 20, milk: 17, bread: 12, doritos: 10 } let total = calculateTotal(document.querySelector('ul.items'), products) console.log(`Total = ${total}`) function calculateTotal(ul, products) { return Array.from(ul.querySelectorAll('li')).reduce((sum, li) => sum + products[li.textContent] || 0, 0) }
 <ul class="items"> <li>oreos</li> <li>milk</li> </ul>

To put you in the right direction (untested):为了让您朝着正确的方向前进(未经测试):

From: https://www.sitepoint.com/community/t/looping-through-li-elements-in-ul/6049/2 and Check if a key exists inside a json object来自: https://www.sitepoint.com/community/t/looping-through-li-elements-in-ul/6049/2检查 json ZA8CFDE6331BD4B62AC96F8911 中是否存在密钥

<ul id="foo">
  <li>First</li>
  <li>Second</li>
  <li>Third</li>
</ul>

var ul = document.getElementById("foo");
var items = ul.getElementsByTagName("li");
for (var i = 0; i < items.length; ++i) {
  // do something with items[i], which is a <li> element
  if((items[i] in products)) alert("yeah");
}

Read this also: Looping through list items with jquery另请阅读: 使用 jquery 遍历列表项

  • This should put you in the right direction (may even work out of the box).这应该使您朝着正确的方向前进(甚至可以开箱即用)。

Trust this helps.相信这会有所帮助。

function getPrice()
{
    let products = {
        oreos: 20,
        milk:  17,
        bread: 12,
        doritos: 10
    }

    var items = document.getElementsByTagName("li");
    var price = 0;

    for (i = 0; i < items.length; i++) {
        var item = items[i];
        var val = parseFloat(products[item.innerHTML]);
        price += val;    
    }

    alert("Total price is: " + String(price));
}

You can then hook the function getPrice to the onclick property of a button to see the result.然后,您可以将 function getPrice挂钩到按钮的 onclick 属性以查看结果。 You may want to give your lists a class, or give the ul an id, so that getPrice doesn't return all the lists on your HTML page.您可能希望给您的列表一个 class,或者给ul一个 id,这样getPrice就不会返回 HTML 页面上的所有列表。

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

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