简体   繁体   English

使用对象内的对象过滤数组

[英]Filtering array with objects inside an object

I have a problem with filtering an array with nested objects. 我有一个使用嵌套对象过滤数组的问题。

[{
    "firstName": "Kevin",
    "lastName": "Smith",
    "expenses": {
      "drink1": 25,
      "drink2": 20
    }
  },
  {
    "firstName": "John",
    "lastName": "Rambo",
    "expenses": {
      "coffe": 10,
      "cake": 20
    }
  }
]

I want to get the objects where the sum of all expenses is > 35. How to get inside expenses ? 我想得到所有费用总和> 35的物品。如何获得内部expenses Or maybe filter is not a proper way here. 或者过滤器在这里可能不合适。

Just filter it, with a condition using reduce to sum the expenses! 只需filter它,使用reduce的条件来计算费用! Pretty straight forward :) 挺直的:)

 const input = [{ "firstName": "Kevin", "lastName": "Smith", "expenses": { "drink1": 26, "drink2": 20 } }, { "firstName": "John", "lastName": "Rambo", "expenses": { "coffe": 10, "cake": 20 } } ]; const output = input.filter(user => Object.values(user.expenses).reduce((acc, expense) => acc + expense) > 45); console.log(output); 

You can try something like this 你可以尝试这样的事情

 var data = [{ "firstName": "Kevin", "lastName": "Smith", "expenses": { "drink1": 25, "drink2": 20 } }, { "firstName": "John", "lastName": "Rambo", "expenses": { "coffe": 10, "cake": 20 } } ] var filtered = data.filter(c => Object.keys(c.expenses) .reduce(function(p, e) { return p + c.expenses[e] }, 0) >= 35 ); console.log(filtered); 

You can access object at index i inside array arr with expression arr[i] What you need to do is to loop over your array. 您可以使用表达式arr[i]访问数组arr中索引i处的对象。您需要做的是遍历数组。 Inside your loop you access each object with expression i have mentioned: arr[i] and then on this object you can access expenses following way arr[i].expenses after this - if i am understanding correctly you sum contents of your arr[i].expenses object and select those objects which satisfy your condition. 在你的循环中你用我提到的表达式访问每个对象: arr[i]然后在这个对象上你可以按照arr[i].expenses之后的方式访问费用。在此之后 - 如果我正确理解你总结你的arr[i].expenses内容arr[i].expenses object并选择那些满足你条件的对象。 Please, See code below: 请看下面的代码:

var expensesAbove35Arr = [];

var yourArray = [
        {
            "firstName": "Kevin",
            "lastName": "Smith",
            "expenses": {
                          "drink1": 26,
                           "drink2": 20
                        }
        },
        {
            "firstName": "John",
            "lastName": "Rambo",
            "expenses": {
                          "coffe": 10,
                           "cake": 20
                        }
        }
];

for(var i=0; i<yourArray.length; i++){
    if(yourArray[i].expenses.coffe + yourArray[i].expenses.cake > 35 ){
        expensesAbove35Arr.push(yourArray[i]);
    }
}

You have got your result inside array expensesAbove35Arr 你已经在数组expensesAbove35Arr得到了你的结果

I'm assuming that you need to keep you array of users the way it is, but with the expenses filtered. 我假设您需要保持用户数量的方式,但过滤费用。

(I assumed wrong as pointed in the comment, keeping this answer just in case someone sees any value on it) (我在评论中指出错误,保留这个答案以防万一有人看到它的任何价值)

Probably this can be optmized or simplified, but here it goes: 可能这可以选择或简化,但在这里:

arr.reduce((acc, user) => [...acc,
  Object.keys(user).reduce((userResult, key) => {
    if (key === 'expenses') {
      return {
         ...userResult,
        expenses: Object.entries(elem.expenses)
          .filter(([product, value]) => value > 35)
          // now "reversing" the object.entries
          .reduce((acc, [product, value]) => ({ [product]: value }), {})
      }
    }
    return {
      ...userResult,
      [key]: elem[key]
    }
  }, user) // starts with the user
], []) //starts with empty array

可能的解决方案可能是:

arr.filter(function(v){ for(expense in v.expenses){ if(v.expenses[expense] > 10){ return true; }else{ return false; } } })

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

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