How To Filter Nested Array In Javascript
This is my json data - { 'list': [ { 'deviceId': '2a-d539-4031-9bfc-4a42f2f765cf', 'versions': [ { 'id': '764c20-a213-9235f4b553b3', '
Solution 1:
Instead of nested filter you could use Array.prototype.some
const obj = {
"list": [
{
"deviceId": "2a-d539-4031-9bfc-4a42f2f765cf",
"versions": [
{
"id": "764c20-a213-9235f4b553b3",
"createdTime": 1590361208034,
"files": [
{
"fileType": "VLAN"
},
{
"fileType": "STARTUPCONFIG",
}
],
"startupRunningStatus": "OUT_OF_SYNC",
"createdBy": "SCHEDULED"
},
{
"id": "9bd33-a45a-ed2fefc46931",
"createdTime": 1589972337717,
"files": [
{
"fileType": "VLAN",
},
{
"fileType": "STARTUPCONFIG",
},
{
"fileType": "RUNNINGCONFIG",
}
],
"startupRunningStatus": "IN_SYNC",
"createdBy": "SCHEDULED_FIRST_TIME"
}
]
}
]
}
let versionsData = obj.list[0].versions;
versionsData = versionsData.filter(
versions =>
versions.files.some(
m => m.fileType === "RUNNINGCONFIG"
) && versions.files.some(
m => m.fileType== 'STARTUPCONFIG'
)
);
console.log(versionsData);
With Array.prototype.filter
const obj = {
"list": [
{
"deviceId": "2a-d539-4031-9bfc-4a42f2f765cf",
"versions": [
{
"id": "764c20-a213-9235f4b553b3",
"createdTime": 1590361208034,
"files": [
{
"fileType": "VLAN"
},
{
"fileType": "STARTUPCONFIG",
}
],
"startupRunningStatus": "OUT_OF_SYNC",
"createdBy": "SCHEDULED"
},
{
"id": "9bd33-a45a-ed2fefc46931",
"createdTime": 1589972337717,
"files": [
{
"fileType": "VLAN",
},
{
"fileType": "STARTUPCONFIG",
},
{
"fileType": "RUNNINGCONFIG",
}
],
"startupRunningStatus": "IN_SYNC",
"createdBy": "SCHEDULED_FIRST_TIME"
}
]
}
]
}
let versionsData = obj.list[0].versions;
versionsData = versionsData.filter( versions => { return versions.files.filter( m => {
return m.fileType === "RUNNINGCONFIG" || m.fileType === "STARTUPCONFIG" }).length > 1 }
);
console.log(versionsData);
Solution 2:
A way with filter inside of filter.
let data = {
"list": [
{
"deviceId": "2a-d539-4031-9bfc-4a42f2f765cf",
"versions": [
{
"id": "764c20-a213-9235f4b553b3",
"createdTime": 1590361208034,
"files": [
{
"fileType": "VLAN"
},
{
"fileType": "STARTUPCONFIG",
}
],
"startupRunningStatus": "OUT_OF_SYNC",
"createdBy": "SCHEDULED"
},
{
"id": "9bd33-a45a-ed2fefc46931",
"createdTime": 1589972337717,
"files": [
{
"fileType": "VLAN",
},
{
"fileType": "STARTUPCONFIG",
},
{
"fileType": "RUNNINGCONFIG",
}
],
"startupRunningStatus": "IN_SYNC",
"createdBy": "SCHEDULED_FIRST_TIME"
}
]
}
]
};
let versionsData = /*response.*/data.list[0].versions;
versionsData = versionsData.filter(versions => {
return versions.files.filter(m => {
return m.fileType == 'RUNNINGCONFIG' || m.fileType == 'STARTUPCONFIG';
}).length == 2;
});
console.log(versionsData);
Solution 3:
const data = {
"list": [
{
"deviceId": "2a-d539-4031-9bfc-4a42f2f765cf",
"versions": [
{
"id": "764c20-a213-9235f4b553b3",
"createdTime": 1590361208034,
"files": [
{
"fileType": "VLAN"
},
{
"fileType": "STARTUPCONFIG",
}
],
"startupRunningStatus": "OUT_OF_SYNC",
"createdBy": "SCHEDULED"
},
{
"id": "9bd33-a45a-ed2fefc46931",
"createdTime": 1589972337717,
"files": [
{
"fileType": "VLAN",
},
{
"fileType": "STARTUPCONFIG",
},
{
"fileType": "RUNNINGCONFIG",
}
],
"startupRunningStatus": "IN_SYNC",
"createdBy": "SCHEDULED_FIRST_TIME"
}
]
}
]
}
const filterData = () => {
return data.list[0].versions.reduce((output, version) => {
const fileTypes = version.files.map(f => f.fileType);
return fileTypes.includes('STARTUPCONFIG') && fileTypes.includes('RUNNINGCONFIG') ?
[...output, version] : output
}, [])
}
console.log(filterData());
Post a Comment for "How To Filter Nested Array In Javascript"