-2

How to loop through the array of objects fruitsnveggies and compare them against the variable fruits and veggies to filter and sum up the values based on the arrays.

I want to create an array of object called totalproduce containing the sum of all quantities of fruits, veggies and others. I tried to loop through the variable fruitsnveggies using a forloop and used if/else conditions to appropriately match to the values from array called fruits, veggies and the leftover elements into others. The issue is I am not sure if I am doing it correctly or is there is a better way to do it.

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},
]

for(var i=0; i<fruitsnveggies.length; i++){
   if(fruitsnveggies[i]["item"] === fruits[i]){
      //Code here
   }else if(fruitsnveggies[i]["item"] === fruits[i]){
      //Code here
   }else{
     //Code here
   }
}

Expected output should be as shown below


var totalproduce = [
  {"items": "fruits", "quantity": 7},
  {"items": "veggies", "quantity": 11},
  {"items": "others", "quantity": 4}
]
Eunice Dhivya
  • 309
  • 4
  • 13

4 Answers4

0

Your code will not work for several reasons:

  • You are checking for equality of the item property with a string at a specific index in the fruits array. This will break once the index goes out of bounds of the fruits array. It will also only check if the item in question is equal to one of the items in fruits and not any of them.
  • You are referencing fruits again in you second condition, when I think you mean to reference veggies.

Here is a better working solution:

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);
Tim Klein
  • 2,538
  • 15
  • 19
0

Using a for loop, which I think is easier to understand and an ES6 simple filter your totalproduce variable would be filled like:

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++;
        }
    }
}
Erwol
  • 1,911
  • 2
  • 23
  • 28
0

You could implement it something like this using the ES6 functions Array.reduce and Array.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)
Linschlager
  • 1,539
  • 11
  • 18
0

try this code jsfiddle to avoid long forEaches and fors and other. With filter, and reduce you can do powerful things. Look methods for Array here

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 onlyVegies = fruitsnveggies
  .filter(a => veggies.includes(a.item))
  .reduce((acc, val) => {
    return {
      item: "veggies",
      quantity: acc.quantity + val.quantity,
    }
  }, {item: "veggies", quantity: 0});

const onlyFruits = fruitsnveggies
  .filter(a => fruits.includes(a.item))
  .reduce((acc, val) => {
    return {
        item: "fruits",
      quantity: acc.quantity + val.quantity,
    }
  }, {item: "fruits", quantity: 0});

  const others = fruitsnveggies
    .filter(a => !fruits.includes(a.item) && !veggies.includes(a.item))
    .reduce((acc, val) => {
      return {
        item: "others",
        quantity: acc.quantity + val.quantity,
      }
    }, {item: "others", quantity: 0});

  console.log("result: ", [onlyVegies, onlyFruits, others])
Franky238
  • 511
  • 5
  • 21