如何过滤多维数组 [英] How to filter a multi-dimensional array

查看:244
本文介绍了如何过滤多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  [
{
type:type1,
docs:[
{
language:EN
},
{
语言:DE
},
{
language:EN
}
]
},
{
type:type2,
docs:[
{
language:EN
}
]









$ b $ ,
{
language:DE
},
{
language:DE
}
]




$ b $ p
$ b

我想过滤它,所以只有docs对象显示了德语的语言。换句话说,我需要:

  [
{
type:type1,
docs:[
{
language:DE
}
]
},
{
type:type2,
docs:[]
},
{
type:type3,
docs:[
语言$ DE

$语言$ DE


$




我的数组最初是由以下代码创建的: / p>

  NSMutableArray * docsArray; 
[self setDocsArray:[downloadedDocsArray mutableCopy]];

我已经尝试循环数组并删除不需要的对象。我已经尝试循环数组,并将想要的对象复制到一个新的数组。我曾尝试使用NSPredicate而不是循环,所有这些都没有成功。

任何人都可以指向正确的方向吗?
$

解决方案

您应该迭代数组以获取包含数组的字典:

  NSArray *结果; 
NSMutableArray * filteredResults = [NSMutableArray new]; //这将存储过滤的项目
//这里你应该已经初始化了
(NSDictionary * dict结果)
{
NSMutableDictionary * mutableDict = [dict mutableCopy] ;
NSArray * docs = dict [@docs];
NSPredicate * predicate = [NSPredicate predicateWithFormat:@language like'DE'];
docs = [docs filteredArrayUsingPredicate:predicate];
[mutableDict setObject:docs forKey:@docs];
[filteredResults addObject:mutableDict];
}


I have the following multi-dimensional array (stripped and reduced for clarity)

[
    {
        "type": "type1",
        "docs": [
            {
                "language": "EN"
            },
            {
                "language": "DE"
            },
            {
                "language": "EN"
            }
        ]
    },
    {
        "type": "type2",
        "docs": [
            {
                "language": "EN"
            }
        ]
    },
    {
        "type": "type3",
        "docs": [
            {
                "language": "FR"
            },
            {
                "language": "DE"
            },
            {
                "language": "DE"
            }
        ]
    }
]

I want to filter it, so only docs objects with a language of DE are shown. In other words, I need this:

[
    {
        "type": "type1",
        "docs": [
            {
                "language": "DE"
            }
        ]
    },
    {
        "type": "type2",
        "docs": []
    },
    {
        "type": "type3",
        "docs": [
            {
                "language": "DE"
            },
            {
                "language": "DE"
            }
        ]
    }
]

My array is initially created by the following piece of code:

NSMutableArray *docsArray;
[self setDocsArray:[downloadedDocsArray mutableCopy]];

I have tried looping the array and removing unwanted objects. I have tried looping the array and copying wanted objects to a new array. I have tried using NSPredicate instead of looping, all of which had very little success.

Can anyone point me in the right direction?

Thanks in advance.

解决方案

You should iterate over the array to get the dictionaries containing the arrays to filter:

NSArray* results;
NSMutableArray* filteredResults= [NSMutableArray new]; // This will store the filtered items
// Here you should have already initialised it 
for( NSDictionary* dict in results)
{
    NSMutableDictionary* mutableDict=[dict mutableCopy];
    NSArray* docs= dict[@"docs"];
    NSPredicate* predicate= [NSPredicate predicateWithFormat: @"language like 'DE'"];
    docs= [docs filteredArrayUsingPredicate: predicate];
    [mutableDict setObject: docs forKey: @"docs"];
    [filteredResults addObject: mutableDict];
}

这篇关于如何过滤多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆