Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 数组中有多少对象类型?_Javascript_Arrays_Function - Fatal编程技术网

Javascript 数组中有多少对象类型?

Javascript 数组中有多少对象类型?,javascript,arrays,function,Javascript,Arrays,Function,我需要帮助循环并计算这个库存阵列中的糖果。他们让我数数糖果的种类(twizzler、sour patch kids、milks duds和now and laters,总共4种)。我需要帮助编写带有返回的函数。我不知道你想数清全部糖果的数量 let inventory=[{ 坎蒂:“抽搐者”, inStock:180, 每周平均:200 }, { 坎蒂:“酸溜溜的孩子们”, 库存量:90, 每周平均:100 }, { 糖果:“牛奶袋”, 库存:300, 每周平均:170 }, { 坎蒂:“现在

我需要帮助循环并计算这个库存阵列中的糖果。他们让我数数糖果的种类(twizzler、sour patch kids、milks duds和now and laters,总共4种)。我需要帮助编写带有返回的函数。

我不知道你想数清全部糖果的数量

let inventory=[{
坎蒂:“抽搐者”,
inStock:180,
每周平均:200
},
{
坎蒂:“酸溜溜的孩子们”,
库存量:90,
每周平均:100
},
{
糖果:“牛奶袋”,
库存:300,
每周平均:170
},
{
坎蒂:“现在和以后”,
库存:150,
每周平均:40
}
];
//在此处编写countCandyTypes函数
函数countCandyTypes(库存){
让总和=库存。减少((acc,elem)=>{
acc+=要素库存;
返回acc;
}, 0)
控制台日志(总和)
}

countCandyTypes(库存)不清楚您要计算什么。如果您只需要不同类型糖果的数量,并且您知道数组中的每个对象都代表一种独特类型的糖果,那么这将起作用:

...
let inventory = [
  { candy: "Twizzlers", inStock: 180, weeklyAverage: 200 },
  { candy: "Sour Patch Kids", inStock: 90, weeklyAverage: 100 },
  { candy: "Milk Duds", inStock: 300, weeklyAverage: 170 },
  { candy: "Now and Laters", inStock: 150, weeklyAverage: 40 }
];

// write the countCandyTypes function here
function countCandyTypes(inventory) {
}
...
function countCandyTypes(inventory) {
  return inventory.length;
}
如果可以有多个对象表示同一类型的糖果,那么这将起作用:

...
let inventory = [
  { candy: "Twizzlers", inStock: 180, weeklyAverage: 200 },
  { candy: "Sour Patch Kids", inStock: 90, weeklyAverage: 100 },
  { candy: "Milk Duds", inStock: 300, weeklyAverage: 170 },
  { candy: "Now and Laters", inStock: 150, weeklyAverage: 40 }
];

// write the countCandyTypes function here
function countCandyTypes(inventory) {
}
...
function countCandyTypes(inventory) {
  return inventory.length;
}
如果你真的只关心糖果的数量而不关心糖果的种类:

function countCandyTypes(inventory) {
  var types = [];
  for (const record of inventory) {
    if (!types.includes(record.candy) {
      types.push(record.candy);
    }
  }
  return types.length;
}

分享你的尝试。你需要真正尝试一些东西。这一问题在其他SO帖子中广泛讨论。谷歌“如何迭代json数组”并从那里开始!这确实奏效了。如果我想要一个做totalInStock的函数呢?我需要的信息不仅仅是
totalInStock
。我发布的第三个函数可能会回答这个问题。如果没有,是否向函数传递了一个参数,如
totalInStock(typeOfCandy)