數(shù)據(jù)結(jié)構(gòu)是這樣子的:
思路:數(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}
]
]