接口數(shù)據(jù)過濾處理之實戰(zhàn)1

數(shù)據(jù)結(jié)構(gòu)是這樣子的:

01.png

思路:數(shù)組很深萎馅,想要過濾最深的項论皆,首先使用map一層一層遍歷殖演,拿到對應(yīng)數(shù)組后熄浓,使用filter()方法return返回符合條件的數(shù)組項情臭。
注意:map遍歷改變引用類型的數(shù)組時,會改變原數(shù)組赌蔑。所以使用:
“const obj = { ...item, list:item.list.filter(item => {return item.checked == 1})};
return obj;”
避免原數(shù)組發(fā)生改變俯在。
對比1和2.
1、我想拿到data下checked = 1的數(shù)據(jù)娃惯,把checked = 0的過濾掉

var carListUpdate = [
{"name":"車輛1","list":[
{"id":10,"number":"74201050","checked":1},
{"id":11,"number":"74201051","checked":0},
{"id":12,"number":"74201052","checked":0},
{"id":13,"number":"74201053","checked":0},
{"id":14,"number":"74201054","checked":0},
{"id":15,"number":"74201055","checked":0},
{"id":16,"number":"74201056","checked":0},
{"id":17,"number":"74201057","checked":0},
{"id":43,"number":"74201058","checked":0}
]},
{"name":"車輛2","list":[
{"id":18,"number":"73602018","checked":1},
{"id":19,"number":"73602019","checked":0},
{"id":20,"number":"73602026","checked":0},
{"id":21,"number":"73602027","checked":0},
{"id":22,"number":"73602048","checked":0},
{"id":23,"number":"73602059","checked":0},
{"id":24,"number":"73602060","checked":0},
{"id":25,"number":"73602080","checked":0},
{"id":26,"number":"73602081","checked":0},
{"id":27,"number":"73602102","checked":0},
{"id":28,"number":"73602103","checked":0}
]},
{"name":"車輛3","list":[
{"id":30,"number":"60100760","checked":1},
{"id":29,"number":"","checked":0},
{"id":31,"number":"60101028","checked":0},
{"id":32,"number":"60101068","checked":0},
{"id":33,"number":"60101069","checked":0},
{"id":34,"number":"60101040","checked":0},
{"id":35,"number":"60101041","checked":0},
{"id":36,"number":"60101078","checked":0},
{"id":37,"number":"60101079","checked":0},
{"id":38,"number":"60101055","checked":0},
{"id":39,"number":"60101058","checked":0},
{"id":40,"number":"60101059","checked":0},
{"id":41,"number":"60101060","checked":0},
{"id":42,"number":"60101065","checked":0}
]}
]
let tempCars = carListUpdate.map(item =>item).map(item => {
          const obj = { ...item, list:item.list.filter(item => {return item.checked == 1})};
          return obj;
     })
console.log(tempCars)

結(jié)果:

tempCars = [
{"name":"車輛1","list":[
{"id":10,"number":"74201050","checked":1}
]},
{"name":"車輛2","list":[
{"id":18,"number":"73602018","checked":1}
]},
{"name":"車輛3","list":[
{"id":30,"number":"60100760","checked":1}
]}
]

2跷乐、我想只想拿到checked = 1 的數(shù)組項,其他的過濾掉

var carDetail = [
    {"name":"車輛1","list":[
        {"id":28,"lp_number":"C6G611","checked":1},
        {"id":30,"lp_number":"RRW392","checked":0},
        {"id":31,"lp_number":"AN3996","checked":0},
        {"id":32,"lp_number":"B17930","checked":0},
        {"id":33,"lp_number":"LW3885","checked":0},
        {"id":34,"lp_number":"RB1631","checked":0},
        {"id":35,"lp_number":"AN8069","checked":0},
        {"id":36,"lp_number":"B33252","checked":0},
        {"id":37,"lp_number":"AD3912","checked":0},
        {"id":39,"lp_number":"B03933","checked":0},
        {"id":40,"lp_number":"AS6931","checked":0},
        {"id":41,"lp_number":"AX1226","checked":0},
        {"id":42,"lp_number":"AF6130","checked":0},
        {"id":43,"lp_number":"AX1109","checked":0},
        {"id":44,"lp_number":"AX1012","checked":0},
        {"id":45,"lp_number":"B38572","checked":0},
        {"id":46,"lp_number":"C22593","checked":0},
        {"id":47,"lp_number":"C22683","checked":0},
        {"id":48,"lp_number":"CA8051","checked":0},
        {"id":49,"lp_number":"CA2127","checked":0},
        {"id":50,"lp_number":"CA9950","checked":0},
        {"id":51,"lp_number":"CA1193","checked":0}
    ]},
    {"name":"車輛2","list":[
        {"id":10,"number":"74201050","checked":1},
        {"id":17,"number":"74201057","checked":0},
        {"id":43,"number":"74201058","checked":0}
    ]},
    {"name":"車輛3","list":[
        {"id":20,"number":"73602026","checked":1},
        {"id":22,"number":"73602048","checked":0},
        {"id":26,"number":"73602081","checked":0},
        {"id":27,"number":"73602102","checked":0},
        {"id":28,"number":"73602103","checked":0}
    ]},
    {"name":"車輛4","list":[
        {"id":29,"number":"","checked":1},
        {"id":30,"number":"60100760","checked":0},
        {"id":32,"number":"60101068","checked":0},
        {"id":36,"number":"60101078","checked":0},
        {"id":37,"number":"60101079","checked":0},
        {"id":40,"number":"60101059","checked":0},
        {"id":41,"number":"60101060","checked":0}
    ]}
]

const xx = carDetail.map(item =>item).map(item => item.list.filter(item => {return item.checked == 1}))
console.log(JSON.stringify(xx))

結(jié)果:

[
    [
        {"id":28,"lp_number":"C6G611","checked":1}
    ],
    [
        {"id":10,"number":"74201050","checked":1}
    ],
    [
        {"id":20,"number":"73602026","checked":1}
    ],
    [
        {"id":29,"number":"","checked":1}
    ]
]
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末趾浅,一起剝皮案震驚了整個濱河市愕提,隨后出現(xiàn)的幾起案子馒稍,更是在濱河造成了極大的恐慌,老刑警劉巖浅侨,帶你破解...
    沈念sama閱讀 211,123評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件纽谒,死亡現(xiàn)場離奇詭異,居然都是意外死亡如输,警方通過查閱死者的電腦和手機(jī)鼓黔,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,031評論 2 384
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來不见,“玉大人澳化,你說我怎么就攤上這事∥人保” “怎么了缎谷?”我有些...
    開封第一講書人閱讀 156,723評論 0 345
  • 文/不壞的土叔 我叫張陵,是天一觀的道長盖高。 經(jīng)常有香客問我慎陵,道長,這世上最難降的妖魔是什么喻奥? 我笑而不...
    開封第一講書人閱讀 56,357評論 1 283
  • 正文 為了忘掉前任席纽,我火速辦了婚禮,結(jié)果婚禮上撞蚕,老公的妹妹穿的比我還像新娘润梯。我一直安慰自己,他們只是感情好甥厦,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,412評論 5 384
  • 文/花漫 我一把揭開白布纺铭。 她就那樣靜靜地躺著,像睡著了一般刀疙。 火紅的嫁衣襯著肌膚如雪舶赔。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,760評論 1 289
  • 那天谦秧,我揣著相機(jī)與錄音竟纳,去河邊找鬼。 笑死疚鲤,一個胖子當(dāng)著我的面吹牛锥累,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播集歇,決...
    沈念sama閱讀 38,904評論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼桶略,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起际歼,我...
    開封第一講書人閱讀 37,672評論 0 266
  • 序言:老撾萬榮一對情侶失蹤惶翻,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后蹬挺,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體维贺,經(jīng)...
    沈念sama閱讀 44,118評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,456評論 2 325
  • 正文 我和宋清朗相戀三年巴帮,在試婚紗的時候發(fā)現(xiàn)自己被綠了溯泣。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,599評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡榕茧,死狀恐怖垃沦,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情用押,我是刑警寧澤肢簿,帶...
    沈念sama閱讀 34,264評論 4 328
  • 正文 年R本政府宣布,位于F島的核電站蜻拨,受9級特大地震影響池充,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜缎讼,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,857評論 3 312
  • 文/蒙蒙 一收夸、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧血崭,春花似錦卧惜、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,731評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至舰讹,卻和暖如春茅姜,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背月匣。 一陣腳步聲響...
    開封第一講書人閱讀 31,956評論 1 264
  • 我被黑心中介騙來泰國打工匈睁, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人桶错。 一個月前我還...
    沈念sama閱讀 46,286評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像胀蛮,于是被迫代替她去往敵國和親院刁。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,465評論 2 348

推薦閱讀更多精彩內(nèi)容