Arrays Angular 8:如何在localStorage中过滤JSON数据

Arrays Angular 8:如何在localStorage中过滤JSON数据,arrays,json,angular,filter,collections,Arrays,Json,Angular,Filter,Collections,我在Angular 8项目中工作,我在localStorage中存储一些数据,我需要对其进行过滤,我不确定我必须使用哪种方法 以下是localStorage值的外观: [{id: 9, designation: "spectacle + restaurant", gift: [], type: "Soirée Bon plan",…},…] 0: {id: 9, designation: "spectacle + restaurant", gift: [], type: "Soirée Bon

我在Angular 8项目中工作,我在localStorage中存储一些数据,我需要对其进行过滤,我不确定我必须使用哪种方法

以下是localStorage值的外观:

[{id: 9, designation: "spectacle + restaurant", gift: [], type: "Soirée Bon plan",…},…]
0: {id: 9, designation: "spectacle + restaurant", gift: [], type: "Soirée Bon plan",…}
amount: 60
banner: ""
designation: "spectacle + restaurant"
format: "bb"
gift: []
icon: "/uploads/fe633f32ab883aae44f154f3fead6982.png"
id: 9
qte: 2
type: "Soirée Bon plan"
1: {id: 10, designation: "spectacle + restaurant", gift: [], type: "Soirée Détente",…}
amount: 80
banner: ""
designation: "spectacle + restaurant"
format: "A4"
gift: []
icon: "/uploads/9b44814145c711307c718cdaf87dc4e2.png"
id: 10
qte: 2
type: "Soirée Détente"
2: {,…}
placesListContent: [{tr_id: "628215", tr_sp_id: "12055", tr_entite: "AG", tr_cat_ordre: "1", tr_cat: "Tarifs :",…}]
qte: 1
restaurant: null
spectacle: {sp_id: "12055", sp_date: "1551737475", sp_th_id: "1264", sp_cat_id: "3", sp_prod_id: "0",…}
3: {,…}
placesListContent: [,…]
qte: 1
restaurant: null
spectacle: {sp_id: "7903", sp_date: "1448480535", sp_th_id: "1254", sp_cat_id: "6", sp_prod_id: "1481",…}
我只需要获得表示“GiftType对象”的元素(这里的第一个和第二个元素),我通过这个函数获得了这些元素:

addGiftToCard(type) {
    type = type.type
    let cardParse = JSON.parse(localStorage.getItem('cart')) || []
    let index = _.findIndex(cardParse, item => item.type && item.id == type.id)
    if (index == -1) {
      type.qte = 1
      cardParse.push(type)
    } else {
      cardParse[index].qte += 1
    }
    localStorage.setItem('cart', JSON.stringify(cardParse))

  }

前两个元素必须通过“qte”属性进行过滤和分隔

如果您的
json
如下所示,则可以使用
filter
方法,其中对现有的
gift
type
属性进行检查:

让arr=[
{id:9,名称:“奇观+餐厅”,礼物:[],类型:“Soirée Bon plan”},
{id:10,名称:“奇观+餐厅”,礼物:[],类型:“Soirée Bon plan”},
{id:11,名称:“奇观+餐厅”}
]
const result=arr.filter(a=>a.gift&&a.type);
控制台日志(结果)