【问题标题】:How to filter the object inside object in angular 2?如何以角度 2 过滤对象内部的对象?
【发布时间】:2018-01-01 13:50:30
【问题描述】:

我有一个这样的 JSON:

filteredArray = [
    {
    "Key":"Brochure",
    "Value":[{
            "Title":"Accounting Services",
            "service":"Accounting",
            "Location":"",
            "Industry":"Accounting",
            "Reprint":"Request",
            "Contact":"Mike Astrup",
            "Updated":"04/15/2017",
            "Owner":"EB",
            "Status":"Approved",
            "online":"true",
            "Type":"Material",
            "Url":".common/service"
        },
        {
            "Title":"Accounting Services",
            "service":"Accounting",
            "Location":"",
            "Industry":"",
            "Reprint":"Request",
            "Contact":"Mike Astrup 1",
            "Updated":"04/15/2017",
            "Owner":"EB",
            "Status":"Approved",
            "online":"true",
            "Type":"Material",
            "Url":".common/service"
        }]
    },
    {
    "Key":"Handout",
    "Value":[{
            "Title":"Accounting Services",
            "service":"Accounting",
            "Location":"",
            "Industry":"",
            "Reprint":"Request",
            "Contact":"Mike Astrup 2",
            "Updated":"04/15/2017",
            "Owner":"EB",
            "Status":"Approved",
            "online":"true",
            "Type":"Material",
            "Url":".common/service"
        },
        {
            "Title":"Accounting Services",
            "service":"Accounting",
            "Location":"",
            "Industry":"",
            "Reprint":"Request",
            "Contact":"Mike Astrup 3",
            "Updated":"04/15/2017",
            "Owner":"EB",
            "Status":"Approved",
            "online":"true",
            "Type":"Material",
            "Url":".common/service"
        }]
    }
]

我必须在 Angular 2 中根据工业基础过滤数据。

我在 Angular 2 的管道中使用此查询,但数据没有被过滤。

filteredArray.filter(
    item => item.Value.filter(
        innerItem => innerItem.Industry.match(industry)))

【问题讨论】:

  • 行业过滤器是什么意思?是否需要按特定行业类型进行分组?
  • 你想达到什么目的?从代码中发布所需的输出
  • 我必须以相同格式过滤此 json 数据,但应根据行业过滤,即如果行业与会计匹配。

标签: angular filter


【解决方案1】:

需要从两个过滤器返回数据:

var filteredArray = [
    {
    "Key":"Brochure",
    "Value":[{
            "Title":"Accounting Services",
            "service":"Accounting",
            "Location":"",
            "Industry":"Accounting",
            "Reprint":"Request",
            "Contact":"Mike Astrup",
            "Updated":"04/15/2017",
            "Owner":"EB",
            "Status":"Approved",
            "online":"true",
            "Type":"Material",
            "Url":".common/service"
        },
        {
            "Title":"Accounting Services",
            "service":"Accounting",
            "Location":"",
            "Industry":"",
            "Reprint":"Request",
            "Contact":"Mike Astrup 1",
            "Updated":"04/15/2017",
            "Owner":"EB",
            "Status":"Approved",
            "online":"true",
            "Type":"Material",
            "Url":".common/service"
        }]
    },
    {
    "Key":"Handout",
    "Value":[{
            "Title":"Accounting Services",
            "service":"Accounting",
            "Location":"",
            "Industry":"",
            "Reprint":"Request",
            "Contact":"Mike Astrup 2",
            "Updated":"04/15/2017",
            "Owner":"EB",
            "Status":"Approved",
            "online":"true",
            "Type":"Material",
            "Url":".common/service"
        },
        {
            "Title":"Accounting Services",
            "service":"Accounting",
            "Location":"",
            "Industry":"",
            "Reprint":"Request",
            "Contact":"Mike Astrup 3",
            "Updated":"04/15/2017",
            "Owner":"EB",
            "Status":"Approved",
            "online":"true",
            "Type":"Material",
            "Url":".common/service"
        }]
    }
]

var test = filteredArray.filter((item) => {
		return item.Value.filter((Initem) => {
			return (Initem.Industry == "Accounting") //Filter for Accounting industry
	})
})

console.log(test);

【讨论】:

  • 感谢您的建议。但这不起作用,因为我也在使用相同的方法,但数据没有被过滤。
  • @AyoushKohli 这是一个工作样本。我不知道你怎么能说它不起作用?
【解决方案2】:

利用Array.filterArray.some

let s = this.data.filter((d) => {
      d.Value = d.Value.filter(d1 => {
        if (d1.Industry === "Accounting") { // here put your condition
          return d1;
        }
      });
      if (d.Value.length > 0) {
        return d;
      }
    });

var data = [
    {
    "Key":"Brochure",
    "Value":[{
            "Title":"Accounting Services",
            "service":"Accounting",
            "Location":"",
            "Industry":"Accounting",
            "Reprint":"Request",
            "Contact":"Mike Astrup",
            "Updated":"04/15/2017",
            "Owner":"EB",
            "Status":"Approved",
            "online":"true",
            "Type":"Material",
            "Url":".common/service"
        },
        {
            "Title":"Accounting Services",
            "service":"Accounting",
            "Location":"",
            "Industry":"",
            "Reprint":"Request",
            "Contact":"Mike Astrup 1",
            "Updated":"04/15/2017",
            "Owner":"EB",
            "Status":"Approved",
            "online":"true",
            "Type":"Material",
            "Url":".common/service"
        }]
    },
    {
    "Key":"Handout",
    "Value":[{
            "Title":"Accounting Services",
            "service":"Accounting",
            "Location":"",
            "Industry":"",
            "Reprint":"Request",
            "Contact":"Mike Astrup 2",
            "Updated":"04/15/2017",
            "Owner":"EB",
            "Status":"Approved",
            "online":"true",
            "Type":"Material",
            "Url":".common/service"
        },
        {
            "Title":"Accounting Services",
            "service":"Accounting",
            "Location":"",
            "Industry":"",
            "Reprint":"Request",
            "Contact":"Mike Astrup 3",
            "Updated":"04/15/2017",
            "Owner":"EB",
            "Status":"Approved",
            "online":"true",
            "Type":"Material",
            "Url":".common/service"
        }]
    }
];

var s = data.filter((d) => {
	 d.Value = d.Value.filter(d1 => { 
   if(d1.Industry === "Accounting") {
   return d1;
   }
   });
   if(d.Value.length > 0){
   return d;
   }
});
console.log(s);

【讨论】:

  • 这不起作用,因为我也在使用相同的方法,但如果您检查,数据不会被过滤。
  • @AyoushKohli 尝试运行它在这里工作的 sn-p 并且您忘记返回我正在使用的内部结果,而您正在使用过滤器这些是差异
  • 结果应该只有一个结果,但根据您的脚本,结果是两个错误的对象。
  • 应该是什么结果
  • :如果您检查 json,结果应该只有一个对象,只有一个行业与会计匹配,但根据您的脚本,结果是两个错误的对象。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-21
  • 1970-01-01
  • 1970-01-01
  • 2021-09-18
  • 1970-01-01
  • 2017-10-24
  • 1970-01-01
相关资源
最近更新 更多