简体   繁体   English

JavaScript:这可能吗? 使用数组值显示数量

[英]JavaScript: Is this possible? using array value to display the quantity

Given this JavaScript array: 鉴于此JavaScript数组:

var list = [1,1,1,2,2,2,2]

I want to know how I can produce an HTML list below that has each unique item in the array and number of times that they appear in the array. 我想知道如何生成下面的HTML列表,该列表具有数组中的每个唯一项以及它们在数组中出现的次数。 I just want to know the JavaScript to produce the data, I can generate the HTML. 我只想知道JavaScript来生成数据,我可以生成HTML。

  • 1 is x3 1是x3
  • 2 is x4 2是x4

I'm confused about how to achieve this. 我对如何实现这一目标感到困惑。 Basically, similar to shopping cart quantity functionality, but using the array. 基本上类似于购物车数量功能,但使用数组。

http://jsfiddle.net/37ab3k00/ http://jsfiddle.net/37ab3k00/

Use .reduce to reduce your array to an object of quantities. 使用.reduce可以将数组缩小为数量对象。

 var list = [1, 1, 1, 2, 2, 2, 2]; var quantities = list.reduce(function(obj, n) { if (obj[n]) obj[n]++; else obj[n] = 1; return obj; }, {}); var ul = document.querySelector("ul"); for (var n in quantities) { ul.appendChild(document.createElement("li")).textContent = n + " has a quantity x" + quantities[n]; } 
 <ul></ul> 

The first argument to .reduce() is a function that gets invoked for each member in the Array. .reduce()的第一个参数是为数组中的每个成员调用的函数。

The second argument is an object that we're going to pass along to each iteration. 第二个参数是一个对象,我们将传递给每个迭代。 It gets passed as the first argument to the function we provided, and we always return it so that it always gets passed as that argument. 它作为我们提供的函数的第一个参数传递,并且我们始终返回它,以便始终将其作为该参数传递。 This is called the accumulator. 这称为累加器。

The n argument to the function we provided is the value of the current member in the list. 我们提供的函数的n参数是列表中当前成员的值。 So what we do is first see if our obj has a truthy n member. 因此,我们要做的是首先查看obj是否具有true n成员。 If so, it must have been encountered already, so we increment it. 如果是这样,则必须已经遇到过,因此我们将其递增。 If not, we assign it the initial value of 1 to represent the first n that was found for that value. 如果不是,我们给它赋初始值1以代表该值的第一个n

var list = [1,1,1,2,2,2,2]

var counts = {};
for (var i = 0; i < list.length; i++) {
    counts[list[i]] = 1 + (counts[list[i]] || 0);
}

Demo: http://jsfiddle.net/famn4zcL/2/ 演示: http//jsfiddle.net/famn4zcL/2/

Add to HTML 新增至HTML

var li = '';

for (var el in counts) {
    li += '<li>' + el + ' is x' + counts[el] + '</li>';  
} 

document.getElementById('list').innerHTML = li;

Demo: http://jsfiddle.net/famn4zcL/3/ 演示: http//jsfiddle.net/famn4zcL/3/

Another way would be using array of objects (those can be easily upgraded with additional data that you probably would need building products), like so: 另一种方法是使用对象数组(可以使用您可能需要构建产品的其他数据轻松升级对象),如下所示:

HTML: HTML:

<span id="display"></span>

JS (plain, no Framework): JS(纯语言,无框架):

var objects = [
    {prod:0,quant:00},
    {prod:1,quant:11},
    {prod:2,quant:22},
    {prod:3,quant:33},
    {prod:4,quant:44},
    {prod:5,quant:55}
];

var list_of_objects = "", display_id = document.getElementById("display");

for (var key in objects) {
  if (objects.hasOwnProperty(key)) {
    console.log(key);
    list_of_objects += '<li>'+objects[key].prod + ' has a qtty x ' + objects[key].quant+'</li>';
  }
}
console.log(list_of_objects);
display_id.innerHTML = list_of_objects;

So you could easily upgrade product data with new info, like: 因此,您可以轻松地使用新信息升级产品数据,例如:

var objects = [
    {prod:0,quant:00,url:"http://url00"},
    {prod:1,quant:11,url:"http://url11"},
    {prod:2,quant:22,url:"http://url22"},
    {prod:3,quant:33,url:"http://url33"},
    {prod:4,quant:44,url:"http://url44"},
    {prod:5,quant:55,url:"http://url55"}
];

JSfiddle to play with: http://jsfiddle.net/7hokfmdu/ JSfiddle可以玩: http : //jsfiddle.net/7hokfmdu/

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

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