从对象数组中的数组中查找项目(水果)列表,并计算其数量并将其作为对象放入另一个数组中

时间:2019-05-15 12:39:44

标签: javascript arrays json

如何遍历对象的水果数组并将它们与可变的水果和蔬菜进行比较,以基于数组对值进行过滤和求和。

我想创建一个称为totalproduce的对象数组,其中包含所有水果,蔬菜等的总量。我尝试使用forloop遍历变量fruitsnveggies,并使用if / else条件来与数组中的值正确匹配,这些数组称为fruits,veggies和剩余元素到其他元素。问题是我不确定我是否做得正确,还是有更好的方法来做到这一点。

NoodlesVendingMachine

预期输出应如下所示

<input type="text" class="myText" />
<span class="text-danger"></span>

<input type="text" class="myText" />
<span class="text-danger"></span>

<script>
    $(".myText").each(function(){
         $(this).siblings("span").eq(0).text("someText");   
    })
</script>

4 个答案:

答案 0 :(得分:0)

您的代码由于以下几个原因而无法正常工作:

  • 您正在检查item属性是否与fruits数组中特定索引处的字符串相等。一旦索引超出fruits数组的边界,这将中断。它还只会检查所讨论的item是否等于fruits中的一项,而不是其中的任何一项。
  • 当您认为要引用fruits时,您在第二种情况下又引用了veggies

这是一个更好的解决方案:

const fruits = ["apples", "oranges", "mango"]

const veggies = ["carrots", "onions", "brocoli"]

const fruitsnveggies = [
    { "item": "apples", "quantity": 3},
    { "item": "oranges", "quantity": 2},
    { "item": "carrots", "quantity": 5},
    { "item": "mango", "quantity": 2},
    { "item": "brocoli", "quantity": 3},
    { "item": "chillipowder", "quantity": 3},
    { "item": "onions", "quantity": 3},
    { "item": "ketchup", "quantity": 1},
]

const totalProduce = {
  "fruits": 0,
  "veggies": 0,
  "other" : 0
}

fruitsnveggies.forEach(
  (produce) => {
    if (fruits.includes(produce["item"])) {
      totalProduce["fruits"] = totalProduce["fruits"] + produce["quantity"];
    } else if (veggies.includes(produce["item"])) {
      totalProduce["veggies"] = totalProduce["veggies"] + produce["quantity"];
    } else {
      totalProduce["other"] = totalProduce["other"] + produce["quantity"];
    }
  }
);

console.log(totalProduce);

答案 1 :(得分:0)

使用一个for循环,我认为它更易于理解,并且ES6简单filter您的totalproduce变量将被填充为:

var fruits = ["apples", "oranges", "mango"];

var veggies = ["carrots", "onions", "brocoli"];

var fruitsnveggies = [
    { "item": "apples", "quantity": 3},
    { "item": "oranges", "quantity": 2},
    { "item": "carrots", "quantity": 5},
    { "item": "mango", "quantity": 2},
    { "item": "brocoli", "quantity": 3},
    { "item": "chillipowder", "quantity": 3},
    { "item": "onions", "quantity": 3},
    { "item": "ketchup", "quantity": 1},
];

var totalproduce = [
    {"items": "fruits", "quantity": 0},
    {"items": "veggies", "quantity": 0},
    {"items": "others", "quantity": 0}
]

for(var i=0; i < fruitsnveggies.length; i++) {
    var element = fruitsnveggies[i];
    var isAFruit = fruits.find(fruit => fruit == element.item[0]);
    if (isAFruit) {
        totalproduce.items[0].quantity++;
    }
    else {
        var isAVeggie = veggies.find(veggie => veggie == element.item[0]);
        if (isAVeggie) {
            totalproduce.items[1].quantity++;
        }
        else {
            totalproduce.items[2].quantity++;
        }
    }
}

答案 2 :(得分:0)

您可以使用ES6函数Array.reduceArray.includes来实现此目的。

var fruits = ["apples", "oranges", "mango"]
    
var veggies = ["carrots", "onions", "brocoli"]

var fruitsnveggies = [
  { "item": "apples", "quantity": 3},
  { "item": "oranges", "quantity": 2},
  { "item": "carrots", "quantity": 5},
  { "item": "mango", "quantity": 2},
  { "item": "brocoli", "quantity": 3},
  { "item": "chillipowder", "quantity": 3},
  { "item": "onions", "quantity": 3},
  { "item": "ketchup", "quantity": 1},
]

var totalProduce = fruitsnveggies.reduce((map, next) => {
  if (fruits.includes(next.item))
      map.fruits += next.quantity;
  else if (veggies.includes(next.item))
      map.veggies += next.quantity;
  else map.other += next.quantity;
  return map
}, { fruits: 0, veggies: 0, other: 0 })

console.log(totalProduce)

答案 3 :(得分:0)

尝试使用此代码jsfiddle来避免长时间fileA.forEach等。使用forfilter,您可以做功能强大的事情。查找reduce here

的方法
Array