用于本机反应的 lodash 数组过滤器
array filter with lodash for react native
我有一个如下所示的数组对象:
[{
sellerId: "seller1",
name: "Nike",
item: [{
id: "xxx1"
isChecked: false
price: "719700.00"
productName: "product a"
quantity: 2
},
{
id: "xxx2"
isChecked: true
price: "219700.00"
productName: "product b"
quantity: 1
}
],
},
{
sellerId: "seller2",
name: "Adidas",
item: [{
id: "xxx1"
isChecked: false
price: "49700.00"
productName: "product x"
quantity: 1
},
{
id: "xxx2"
isChecked: true
price: "4700.00"
productName: "product y"
quantity: 5
}
],
},
]
现在我想在名为 Item 的对象上通过 isChecked = true 转换或过滤我的数据,因此它看起来像这样:
[{
sellerId: "seller1",
name: "Nike",
item: [
{
id: "xxx2"
isChecked: true
price: "219700.00"
productName: "product b"
quantity: 1
}
],
},
{
sellerId: "seller2",
name: "Adidas",
item: [
{
id: "xxx2"
isChecked: true
price: "4700.00"
productName: "product y"
quantity: 5
}
],
},
]
已编辑:
然后如果一个卖家没有isChecked: true,它不会显示卖家数据,例如如果阿迪达斯卖家没有isChecked,我希望对象看起来像这样:
[{
sellerId: "seller1",
name: "Nike",
item: [
{
id: "xxx2"
isChecked: true
price: "219700.00"
productName: "product b"
quantity: 1
}
],
},
]
我试过像这样使用 lodash,但它仍然没有删除具有 isChecked: false 的项目
_.filter(data, { item: [{ isChecked: false }] })
关于如何使用 lodash 或简单的 javascript filter() 执行此操作的任何帮助?
谢谢!
您可以使用 filter 和 map 函数:
sellers = data.map((d) => {return {...d, item: d.item.filter(i=>i.isChecked)}})
对于空项目,您可以像这样再次过滤它:
filterd_seller = sellers.filter(seller => seller.item.length)
或者您可以使用更好的答案 reduce:
data.reduce((filtered, seller) => {
const item = seller.item.filter(i => i.isChecked);
if (item.length) {
filtered.push({...seller, item });
}
return filtered;
}, []);
映射对象数组(使用 lodash 或 vanilla),然后在使用 isChecked: false
过滤掉任何项目的同时重新创建对象:
const arr = [{"sellerId":"seller1","name":"Nike","item":[{"id":"xxx1","isChecked":false,"price":"719700.00","productName":"product a","quantity":2},{"id":"xxx2","isChecked":true,"price":"219700.00","productName":"product b","quantity":1}]},{"sellerId":"seller2","name":"Adidas","item":[{"id":"xxx1","isChecked":false,"price":"49700.00","productName":"product x","quantity":1},{"id":"xxx2","isChecked":true,"price":"4700.00","productName":"product y","quantity":5}]}]
const result = arr.map(({ item, ...o }) => ({
...o,
item: item.filter(o => o.isChecked)
}))
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
我有一个如下所示的数组对象:
[{
sellerId: "seller1",
name: "Nike",
item: [{
id: "xxx1"
isChecked: false
price: "719700.00"
productName: "product a"
quantity: 2
},
{
id: "xxx2"
isChecked: true
price: "219700.00"
productName: "product b"
quantity: 1
}
],
},
{
sellerId: "seller2",
name: "Adidas",
item: [{
id: "xxx1"
isChecked: false
price: "49700.00"
productName: "product x"
quantity: 1
},
{
id: "xxx2"
isChecked: true
price: "4700.00"
productName: "product y"
quantity: 5
}
],
},
]
现在我想在名为 Item 的对象上通过 isChecked = true 转换或过滤我的数据,因此它看起来像这样:
[{
sellerId: "seller1",
name: "Nike",
item: [
{
id: "xxx2"
isChecked: true
price: "219700.00"
productName: "product b"
quantity: 1
}
],
},
{
sellerId: "seller2",
name: "Adidas",
item: [
{
id: "xxx2"
isChecked: true
price: "4700.00"
productName: "product y"
quantity: 5
}
],
},
]
已编辑:
然后如果一个卖家没有isChecked: true,它不会显示卖家数据,例如如果阿迪达斯卖家没有isChecked,我希望对象看起来像这样:
[{
sellerId: "seller1",
name: "Nike",
item: [
{
id: "xxx2"
isChecked: true
price: "219700.00"
productName: "product b"
quantity: 1
}
],
},
]
我试过像这样使用 lodash,但它仍然没有删除具有 isChecked: false 的项目
_.filter(data, { item: [{ isChecked: false }] })
关于如何使用 lodash 或简单的 javascript filter() 执行此操作的任何帮助?
谢谢!
您可以使用 filter 和 map 函数:
sellers = data.map((d) => {return {...d, item: d.item.filter(i=>i.isChecked)}})
对于空项目,您可以像这样再次过滤它:
filterd_seller = sellers.filter(seller => seller.item.length)
或者您可以使用更好的答案 reduce:
data.reduce((filtered, seller) => {
const item = seller.item.filter(i => i.isChecked);
if (item.length) {
filtered.push({...seller, item });
}
return filtered;
}, []);
映射对象数组(使用 lodash 或 vanilla),然后在使用 isChecked: false
过滤掉任何项目的同时重新创建对象:
const arr = [{"sellerId":"seller1","name":"Nike","item":[{"id":"xxx1","isChecked":false,"price":"719700.00","productName":"product a","quantity":2},{"id":"xxx2","isChecked":true,"price":"219700.00","productName":"product b","quantity":1}]},{"sellerId":"seller2","name":"Adidas","item":[{"id":"xxx1","isChecked":false,"price":"49700.00","productName":"product x","quantity":1},{"id":"xxx2","isChecked":true,"price":"4700.00","productName":"product y","quantity":5}]}]
const result = arr.map(({ item, ...o }) => ({
...o,
item: item.filter(o => o.isChecked)
}))
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }