[英]How to filter a 2D array based on the value of the inner array
I have a 2D array that I need to filter based on data of the Inner array我有一个二维数组,我需要根据内部数组的数据进行过滤
I have tried mapping then filtering the data of the inner array - but then I lose the data from the first array.我尝试映射然后过滤内部数组的数据 - 但随后我丢失了第一个数组中的数据。
I have written a filter function below but it does not work properly- look at filteredData variable我在下面写了一个过滤器函数,但它不能正常工作-查看filteredData变量
var data =
[
{
"batch_id": "1",
"data": [
{
"identifier": "aa",
"type": "video"
},
{
"identifier": "ab",
"type": "image"
}
],
"internal_batch_id": 72
},
{
"batch_id": "2",
"data": [
{
"identifier": "a",
"type": "image"
},
{
"identifier": "b",
"type": "image"
}
],
"internal_batch_id": 72
}
]
var type = 'video'
var filteredData =
data.map((item, i) => {
return item.data.filter((item1, idx) => {
return item1.type.toUpperCase() === type.toUpperCase()
})
})
What I want to happen is that only the items where type === 'video' are shown.我想要发生的是只显示 type === 'video' 的项目。 So data would become所以数据会变成
{
"batch_id": "1",
"data": [
{
"identifier": "aa",
"type": "video"
}
],
"internal_batch_id": 72
}
I'm not sure what your expected out put is supposed to be.我不确定您的预期输出应该是什么。 Either, you are looking to use map with filter:或者,您希望将地图与过滤器一起使用:
Filter the nested data prop by each type property按每个类型属性过滤嵌套数据道具
var data = [ { batch_id: '1', data: [{ identifier: 'aa', type: 'video' }, { identifier: 'ab', type: 'image' }], internal_batch_id: 72 }, { batch_id: '2', data: [{ identifier: 'a', type: 'image' }, { identifier: 'b', type: 'image' }], internal_batch_id: 72 } ] console.log(data.map(o => ({...o, data:o.data.filter(v => v.type === 'video')})))
Or you are looking to use filter with some:或者您希望将过滤器与一些一起使用:
Filter entire data object to object where it has a type with 'video'将整个数据对象过滤为具有“视频”类型的对象
var data = [ { batch_id: '1', data: [{ identifier: 'aa', type: 'video' }, { identifier: 'ab', type: 'image' }], internal_batch_id: 72 }, { batch_id: '2', data: [{ identifier: 'a', type: 'image' }, { identifier: 'b', type: 'image' }], internal_batch_id: 72 } ] console.log(data.filter(o => o.data.some(v => v.type === 'video')))
Or, both:或两者:
Filter both the entire data object, and filter the nested data.过滤整个数据对象,过滤嵌套数据。
var data = [ { batch_id: '1', data: [{ identifier: 'aa', type: 'video' }, { identifier: 'ab', type: 'image' }], internal_batch_id: 72 }, { batch_id: '2', data: [{ identifier: 'a', type: 'image' }, { identifier: 'b', type: 'image' }], internal_batch_id: 72 } ] console.log( data.reduce((a, o) => { const f = o.data.filter(v => v.type === 'video') return (f.length && a.push({ ...o, data: f }), a) }, []) )
You need to perform 2 operations,您需要执行 2 个操作,
video
element使用至少具有 1 个video
元素的所有对象过滤父列表video
objects inside them.更新上面的对象,使其内部仅包含video
对象。 For this you can use .reduce
+ .filter
为此,您可以使用.reduce
+ .filter
.reduce
to create new object and create array .reduce
创建新对象并创建数组.filter
to get only filtered items in it. .filter
只获取其中过滤的项目。 var data = [{ "batch_id": "1", "data": [{ "identifier": "aa", "type": "video" }, { "identifier": "ab", "type": "image" } ], "internal_batch_id": 72 }, { "batch_id": "2", "data": [{ "identifier": "a", "type": "image" }, { "identifier": "b", "type": "image" } ], "internal_batch_id": 72 }] var type = 'video' var filteredData = data.reduce((acc, obj) => { const innerObj = obj.data.filter((typeObj) => typeObj.type === type); if(innerObj.length !== 0) { acc.push( { ...obj, data: innerObj} ); } return acc; }, []) console.log(filteredData)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.