我需要找到一个特定的项目在JSON响应使用 Postman 工具出现的总数

bis0qfac  于 8个月前  发布在  Postman
关注(0)|答案(3)|浏览(108)
[
    {
        "branchCode": "111",
        "referenceNumber": "2222",
        "comments": "Write Off",
        "transactionDate": "2022-02-23T00:00:00",
        "amount": 0,
        "accountCode": "MMMM",
        "accountName": "Metal MMMM",
        "allocations": [
            {
                "branchCode": "111",
                "referenceNumber": "3333",
                "allocationDate": "2022-02-23T09:23:57",
                "ledgerTypeDescription": "Client",
                "accountCode": "MMMM",
                "accountName": "Metal MMMM",
                "amount": -0.02
            }
        ]
    },
    {
        "branchCode": "222",
        "referenceNumber": "33333",
        "comments": "To write off historic med fee $0",
        "transactionDate": "2022-03-24T00:00:00",
        "amount": 0,
        "accountCode": "OOOO",
        "accountName": "Oranga OOOO - AEP",
        "allocations": [
            {
                "branchCode": "222",
                "referenceNumber": "456",
                "allocationDate": "2022-03-24T10:39:02",
                "ledgerTypeDescription": "Client",
                "accountCode": "OOOOP",
                "accountName": "Oranga 0000 - AEP",
                "amount": 94.88
            }
        ]
    }
]

在上面的JSON响应中,我需要找到在整个响应中完成了多少分配。我可以使用for循环来实现吗?如果可以,如何使用它来遍历数组,然后计算分配项
我尝试使用Object.keys(jsondata[0]).length)方法。但这对于查找特定块中的项的长度很有用。但我不明白如何遍历数组并找到分配的数量

wlwcrazw

wlwcrazw1#

另一种选择是使用flatMap()来获取所有可以调用.lengthallocations

const data = [{"branchCode": "111", "referenceNumber": "2222", "comments": "Write Off", "transactionDate": "2022-02-23T00:00:00", "amount": 0, "accountCode": "MMMM", "accountName": "Metal MMMM", "allocations": [{"branchCode": "111", "referenceNumber": "3333", "allocationDate": "2022-02-23T09:23:57", "ledgerTypeDescription": "Client", "accountCode": "MMMM", "accountName": "Metal MMMM", "amount": -0.02 } ] }, {"branchCode": "222", "referenceNumber": "33333", "comments": "To write off historic med fee $0", "transactionDate": "2022-03-24T00:00:00", "amount": 0, "accountCode": "OOOO", "accountName": "Oranga OOOO - AEP", "allocations": [{"branchCode": "222", "referenceNumber": "456", "allocationDate": "2022-03-24T10:39:02", "ledgerTypeDescription": "Client", "accountCode": "OOOOP", "accountName": "Oranga 0000 - AEP", "amount": 94.88 } ] } ];

const n = data.flatMap(o => o.allocations).length;

console.log(n);
cvxl0en2

cvxl0en22#

你可以像这样使用reduce。请注意,如果接收到的是字符串,则可能需要对数据使用JSON.parse

let jsonData = [
    {
        "branchCode": "111",
        "referenceNumber": "2222",
        "comments": "Write Off",
        "transactionDate": "2022-02-23T00:00:00",
        "amount": 0,
        "accountCode": "MMMM",
        "accountName": "Metal MMMM",
        "allocations": [
            {
                "branchCode": "111",
                "referenceNumber": "3333",
                "allocationDate": "2022-02-23T09:23:57",
                "ledgerTypeDescription": "Client",
                "accountCode": "MMMM",
                "accountName": "Metal MMMM",
                "amount": -0.02
            }
        ]
    },
    {
        "branchCode": "222",
        "referenceNumber": "33333",
        "comments": "To write off historic med fee $0",
        "transactionDate": "2022-03-24T00:00:00",
        "amount": 0,
        "accountCode": "OOOO",
        "accountName": "Oranga OOOO - AEP",
        "allocations": [
            {
                "branchCode": "222",
                "referenceNumber": "456",
                "allocationDate": "2022-03-24T10:39:02",
                "ledgerTypeDescription": "Client",
                "accountCode": "OOOOP",
                "accountName": "Oranga 0000 - AEP",
                "amount": 94.88
            }
        ]
    }
];

let nbAllocations = jsonData.reduce((acc, el) => acc + el.allocations.length, 0);

console.log(nbAllocations);
ajsxfq5m

ajsxfq5m3#

您也可以在Array.forEach()的帮助下实现此要求。
现场演示**:**

const jsonObj = [
    {
        "branchCode": "111",
        "referenceNumber": "2222",
        "comments": "Write Off",
        "transactionDate": "2022-02-23T00:00:00",
        "amount": 0,
        "accountCode": "MMMM",
        "accountName": "Metal MMMM",
        "allocations": [
            {
                "branchCode": "111",
                "referenceNumber": "3333",
                "allocationDate": "2022-02-23T09:23:57",
                "ledgerTypeDescription": "Client",
                "accountCode": "MMMM",
                "accountName": "Metal MMMM",
                "amount": -0.02
            }
        ]
    },
    {
        "branchCode": "222",
        "referenceNumber": "33333",
        "comments": "To write off historic med fee $0",
        "transactionDate": "2022-03-24T00:00:00",
        "amount": 0,
        "accountCode": "OOOO",
        "accountName": "Oranga OOOO - AEP",
        "allocations": [
            {
                "branchCode": "222",
                "referenceNumber": "456",
                "allocationDate": "2022-03-24T10:39:02",
                "ledgerTypeDescription": "Client",
                "accountCode": "OOOOP",
                "accountName": "Oranga 0000 - AEP",
                "amount": 94.88
            }
        ]
    }
];

let count = 0;

jsonObj.forEach(({ allocations }) => count += allocations.length)

console.log(count);

相关问题