第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Javascript 中過濾嵌套對象

如何在 Javascript 中過濾嵌套對象

九州編程 2022-06-16 16:32:01
我將如何在嵌套項目上使用 filter() ?我正在嘗試檢索具有 projex.HeightPay === '1' 的所有數(shù)據(jù)。如果 HeightPay 為 1,我想取回 Id、Name、System 等和項目項目。例如:  const fakeData = [  {    Id: "022173333101",    Name: "Blue",    System: "DESIGN",    Squares: 0,    Attributes: {      projex: [        {          Project: "50",          HeightPay: "1"        },        {          Project: "50",          HeightPay: "0"        }      ]    },    Customer: {      Addr1: "Somewhere",      Addr2: ""    }  }];// returns nothingconst found = fakeData.filter(data => data.Attributes.projex.HeightPay === "1");期望的輸出:  {    Id: "022173333101",    Name: "Blue",    System: "DESIGN",    Squares: 0,    Attributes: {      projex: [        {          Project: "50",          HeightPay: "1"        }      ]    },    Customer: {      Addr1: "Somewhere",      Addr2: ""    }  }
查看完整描述

2 回答

?
交互式愛情

TA貢獻(xiàn)1712條經(jīng)驗 獲得超3個贊

您可以使用Array.prototype.map遍歷數(shù)組的每個元素,然后使用fakeData過濾子數(shù)組并從每次迭代的調(diào)用中返回一個新對象Attributes.projexArray.prototype.filtermap


調(diào)用中的新對象Array.prototype.map是通過使用對象擴(kuò)展運算符獲取每個元素的所有屬性(屬性除外) ,然后將新數(shù)組從分配給每個新對象:fakeData...Attributes.projexAttributes.projexArray.prototype.filter


const fakeData = [ { Id: "022173333101", Name: "Blue", System: "DESIGN", Squares: 0, Attributes: { projex: [ { Project: "50", HeightPay: "1" }, { Project: "50", HeightPay: "0" } ] }, Customer: { Addr1: "Somewhere", Addr2: "" } } ];


const found = fakeData.map(data => ({

  ...data,

  Attributes: {

    projex: data.Attributes.projex.filter(({

      HeightPay

    }) => HeightPay === "1")

  }

}));

console.log(found);


查看完整回答
反對 回復(fù) 2022-06-16
?
鴻蒙傳說

TA貢獻(xiàn)1865條經(jīng)驗 獲得超7個贊

const result = fakeData.map(item => ({

        ...item,

        Attributes: {

                projex: item.Attributes.projex.filter(e => e.HeightPay === "1")

        }

}))


查看完整回答
反對 回復(fù) 2022-06-16
  • 2 回答
  • 0 關(guān)注
  • 279 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號