简体   繁体   English

过滤和排序JSON数组

[英]Filtering and sorting a JSON array

records = [
        {
            name: "Alpha",
            set: 5,
            weight: 185
        },      
        {
            name: "Alpha",
            set: 5,
            weight: 350
        },        
        {
            name: "Bravo",
            set: 5,
            weight: 185
        },        
        {
            name: "Charlie",
            set: 5,
            weight: 185
        },         
        {
            name: "Delta",
            set: 5,
            weight: 185
        }
]

I have a JSON array of multiple records and I need to filter these records by name and weight. 我有多个记录的JSON数组,我需要按名称和权重过滤这些记录。 So for example, since there are two "Alpha" records, I need to only pull in the one with the highest weight (which would be the second record). 因此,例如,由于有两个“ Alpha”记录,因此我只需要提取权重最高的一个(这将是第二个记录)。 I have no idea how to filter and rebuild this array with only the desired results. 我不知道如何过滤和重建仅具有所需结果的数组。

I need to keep the original array intact as I'll be displaying ALL in a table, but I need to build a secondary array with only the objects with the greatest value, by name. 我需要保持原始数组完整无缺,因为我将在表中显示ALL,但是按名称命名,我需要构建仅包含具有最大价值的对象的辅助数组。

You can use lodash 4+ for this. 您可以为此使用lodash 4+。

var sortedArray = _.orderBy(records, ['weight'], ['desc']); var sortedArray = _.orderBy(records,['weight'],['desc']);

This will sort the array by weight. 这将按重量对数组进行排序。 Then , _.uniqBy(sortedArray,'name') 然后, _.uniqBy(sortedArray,'name')

This will return the final array. 这将返回最终数组。

var records = [
        {
            name: "Alpha",
            set: 5,
            weight: 185
        },      
        {
            name: "Alpha",
            set: 5,
            weight: 350
        },        
        {
            name: "Bravo",
            set: 5,
            weight: 185
        },        
        {
            name: "Charlie",
            set: 5,
            weight: 185
        },         
        {
            name: "Delta",
            set: 5,
            weight: 185
        }
]

var sortedArray = _.orderBy(records, ['weight'], ['desc'])

_.uniqBy(sortedArray,'name')

I recommend a functional programming approach: 我推荐一种函数式编程方法:

var newRecords = records
    .filter(function(record) { 
        return records.find(function(innerRecord) {
            return innerRecord.name === record.name && innerRecord.weight > record.weight; }) === undefined;
     });

In this example, you return only records where you cannot find a record sharing the same name but with a larger weight. 在此示例中,仅返回找不到相同名称但权重较大的记录的记录。 Also, your newly re-built array is stored in newRecords , leaving your original array intact. 另外,新重建的数组存储在newRecords ,而原始数组保持不变。

This first sorts the items in name order and weight order and then filter out all but the first of each item. 这首先按名称顺序和权重顺序对项目进行排序,然后过滤掉除每个项目的第一个以外的所有项目。

 var records = [{ name: "Alpha", set: 5, weight: 185 }, { name: "Alpha", set: 5, weight: 350 }, { name: "Bravo", set: 5, weight: 185 }, { name: "Charlie", set: 5, weight: 185 }, { name: "Charlie", set: 5, weight: 200 }, { name: "Delta", set: 5, weight: 185 } ] console.log( records.sort((a, b) => { if (a.name === b.name) { return a.weight >= b.weight ? -1 : 1 } return a.name > b.name ? 1 : -1 }) .filter((rec, i, arr) => { if (i === 0) return true return rec.name !== arr[i - 1].name }) ) 

I recommend you start using lodash and dont spend time to create your own functions. 我建议您开始使用lodash,不要花时间创建自己的函数。 For example, to sort your array by name you should write the following code: 例如,要按名称对数组进行排序,应编写以下代码:

    var records = [
        {
            name: "Alpha",
            set: 5,
            weight: 185
        },      
        {
            name: "Alpha",
            set: 5,
            weight: 350
        },        
        {
            name: "Bravo",
            set: 5,
            weight: 185
        },        
        {
            name: "Charlie",
            set: 5,
            weight: 185
        },         
        {
            name: "Delta",
            set: 5,
            weight: 185
        }
];
var sorted = _.sortBy(records, ['name']);

And second case, to filter by name and weight 第二种情况,按名称和权重进行过滤

var filtered = _.filter(records, [ 'name': 'Alpha', 'weight': 350 ]);

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

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