简体   繁体   English

如何使用Array.filter

[英]How to use Array.filter

If I have a structure 如果我有结构

{
    "users": {
        "John": [1 , 4, 5],
        "Max": [2, 3, 7],
        "Bill": [3, 5, 4]
    }
}

array is months arrears for 3 months. 数组是3个月的欠款。 I need to print who is most of all owed by month. 我需要打印每月欠谁的钱。 Example

  • Bill, Max, John - for the 1st month. 比尔,马克斯,约翰-第一个月。
  • Bill, John, Max - for the 2nd month. 比尔,约翰,马克斯-第二个月。
  • and so on. 等等。

I need to do it with JS. 我需要使用JS。 Is there any simple way to use Array.filter? 有没有使用Array.filter的简单方法?

 var users = [ {name: "John", values: [1 , 4, 5]}, {name: "Max", values: [2, 3, 7]}, {name : "Bill", values: [3, 5, 4]} ] function sortByMonth(monthNumber) { return users.sort(function(a, b) { return b.values[monthNumber] - a.values[monthNumber];}); } 

This should be what you're looking for, you have to actually change your values into an array but then you can sort by amount owed per month with the sortByMonth function. 这应该是您要寻找的内容,您必须将值实际更改为数组,然后才能使用sortByMonth函数按每月的欠款进行排序。

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

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