javascript - Find list of items(fruits) from an array in an array of object and calculate its quantity and push into another array as an object
问题描述
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}
]
解决方案
试试这个代码jsfiddle以避免 long forEach
es 和for
s 等。使用filter
,reduce
你可以做强大的事情。在这里寻找方法Array
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])
推荐阅读
- r - 从 Rmd 编织到仪表板时无法读取我的数据
- docker - 反向代理到另一台机器
- r - 使用列索引号而不是名称在 R 中聚合函数
- docker - docker 在 Windows 10 中显示权限提升错误
- c++ - 不能在类中创建多个对象
- math - 数学问题:考虑支付处理器费用计算要支付的价格
- python-3.x - 使用 train_test_split 并使用 mne CSP 进行交叉验证的不同结果
- c# - C# 将分钟添加到计时器 (WPF/XAML)
- c++ - 如何在 c++ 中使用不同长度的数组初始化嵌套的 2D int 数组(或指向 int[] 的指针数组?)?
- django-models - 模型指的是同一models.py中的另一个类