Filter an array with TypeScript in Angular

EmadKhan
 
on Mar 21, 2021 11:40 PM
Sample_976772.zip
1194 Views

How Can I use Filter() function of typescript to inspect inner property of JSON object

How to get only those nodes whose sales property is true?

Like in the below case I need only id:54 record because there is one department whose sales is true, while ID 56 does not have any department whose sales property is true so it should be neglected.

{
    "id": 54,
    "firstName": "Data",
    "departmentAccess": [
        {
            "departmentId": 1118,
            "departmentName": "Hyundai - New Cars",
            "departmentCode": "HY - NEW",
            "sales": true,
            "trade": true
        },
        {
            "departmentId": 1119,
            "departmentName": "Jaguar & Land Rover - New Cars",
            "departmentCode": "Jag & LR",
            "sales": false,
            "trade": true
        }
    ]
},
{
    "id": 56,
    "firstName": "Peter",
    "departmentAccess": [
        {
            "departmentId": 1118,
            "departmentName": "Hyundai - New Cars",
            "departmentCode": "HY - NEW",
            "sales": false,
            "trade": false
        },
        {
            "departmentId": 1119,
            "departmentName": "Jaguar & Land Rover - New Cars",
            "departmentCode": "Jag & LR",
            "sales": false,
            "trade": false
        }
    ]
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Mar 21, 2021 11:42 PM

I will get back to you soon.

dharmendr
 
on Mar 22, 2021 02:08 AM

Hi EmadKhan,

Refer below code.

HTML

<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <script type="text/javascript">
        window.onload = function () {
            var students = [];
            var student = {};
            student.id = 54;
            student.firstName = "Data";
            var dptAccess = [];
            dptAccess.push({
                "departmentId": 1118,
                "departmentName": "Hyundai - New Cars",
                "departmentCode": "HY - NEW",
                "sales": true,
                "trade": true
            });
            dptAccess.push({
                "departmentId": 1119,
                "departmentName": "Jaguar & Land Rover - New Cars",
                "departmentCode": "Jag & LR",
                "sales": false,
                "trade": true
            });

            student.departmentAccess = dptAccess;
            students.push(student);

            student = {};
            student.id = 56;
            student.firstName = "Peter";
            var dptAccess = [];
            dptAccess.push({
                "departmentId": 1118,
                "departmentName": "Hyundai - New Cars",
                "departmentCode": "HY - NEW",
                "sales": false,
                "trade": false
            });
            dptAccess.push({
                "departmentId": 1119,
                "departmentName": "Jaguar & Land Rover - New Cars",
                "departmentCode": "Jag & LR",
                "sales": false,
                "trade": false
            });

            student.departmentAccess = dptAccess;
            students.push(student);

            var filteredArray = students.filter(function (student) {
                return student.departmentAccess.some(function (dptAccess) {
                    return dptAccess.sales === true
                });
            });
            alert(JSON.stringify(filteredArray));
        };
    </script>
</body>
</html>

Demo