使用括号表示法在javascript中读取json

时间:2016-06-20 11:07:40

标签: javascript arrays json

我有一个要求,我必须读取用户传递的列的JSON数据的值

for (var k = 0; k < arr.length; k++) {
    for (var i = 0; i < checkedFilters.length; i++) {
        console.log("value[columnName]", arr[k][columnName]);
        if ((arr[k][columnName] == checkedFilters[i])) {
            temporaryFilterData.push(arr[k]);
        }
    }
}

columnName将由用户在函数中传递 但是当我尝试记录arr[k][columnName]的值时,它只给出了数组的第一个值。 可能的原因是什么

JSON

 [
        {
            id: 1,
            type: 1,
            typeName: "Lead",
            client: 1,
            clientName: "Ljungbloms Elektriska AB",
            marking: "Marking for Ljungbloms Elektriska AB",
            status: 2,
            statusName: "Open",
            stage: 2,
            stageName: "Stage 2",
            leadValue: 1,
            probability: 1,
            issuer: 1,
            issuerName: "Sales",
            handler: 1,
            handlerName: "Sales",
            created: 1462345200000,
            createdString: "2016-05-04"
        },
        {
            id: 5,
            type: 1,
            typeName: "Lead",
            client: 1,
            clientName: "Ljungbloms Elektriska AB",
            marking: "Marking for Ljungbloms Elektriska AB",
            status: 2,
            statusName: "Open",
            stage: 2,
            stageName: "Stage 2",
            leadValue: 1,
            probability: 1,
            issuer: 1,
            issuerName: "Sales",
            handler: 1,
            handlerName: "Sales",
            created: 1462345200000,
            createdString: "2016-05-04"
        },
        {
            id: 2,
            type: 1,
            typeName: "Lead",
            client: 2,
            clientName: "Solina Sweden AB",
            marking: "Marking for Solina Sweden AB",
            status: 1,
            statusName: "Closed",
            stage: 3,
            stageName: "Stage 3",
            leadValue: 1,
            probability: 1,
            issuer: 1,
            issuerName: "Sales",
            handler: 1,
            handlerName: "Sales",
            created: 1462345200000,
            createdString: "2016-05-04"
        },
        {
            id: 3,
            type: 2,
            typeName: "Opportunity",
            client: 3,
            clientName: "H & M Hennes & Mauritz GBC AB",
            marking: "Marking for H & M Hennes & Mauritz GBC AB",
            status: 3,
            statusName: "Pending",
            stage: 4,
            stageName: "Stage 4",
            leadValue: 1,
            probability: 1,
            issuer: 1,
            issuerName: "Sales",
            handler: 1,
            handlerName: "Sales",
            created: 1462345200000,
            createdString: "2016-05-04"
        }
     ];

checkedFileter数组

["Open", "Unset","Closed"]

结果JSON

[{
                id: 1,
                type: 1,
                typeName: "Lead",
                client: 1,
                clientName: "Ljungbloms Elektriska AB",
                marking: "Marking for Ljungbloms Elektriska AB",
                status: 2,
                statusName: "Open",
                stage: 2,
                stageName: "Stage 2",
                leadValue: 1,
                probability: 1,
                issuer: 1,
                issuerName: "Sales",
                handler: 1,
                handlerName: "Sales",
                created: 1462345200000,
                createdString: "2016-05-04"
            },
            {
                id: 5,
                type: 1,
                typeName: "Lead",
                client: 1,
                clientName: "Ljungbloms Elektriska AB",
                marking: "Marking for Ljungbloms Elektriska AB",
                status: 2,
                statusName: "Open",
                stage: 2,
                stageName: "Stage 2",
                leadValue: 1,
                probability: 1,
                issuer: 1,
                issuerName: "Sales",
                handler: 1,
                handlerName: "Sales",
                created: 1462345200000,
                createdString: "2016-05-04"
            },
            {
                id: 2,
                type: 1,
                typeName: "Lead",
                client: 2,
                clientName: "Solina Sweden AB",
                marking: "Marking for Solina Sweden AB",
                status: 1,
                statusName: "Closed",
                stage: 3,
                stageName: "Stage 3",
                leadValue: 1,
                probability: 1,
                issuer: 1,
                issuerName: "Sales",
                handler: 1,
                handlerName: "Sales",
                created: 1462345200000,
                createdString: "2016-05-04"
            }]

ColumnName 是一个变量,您可以将其理解为包含列名称的简单变量 例如:statusName

所以这应该是这样的,条件将仅检查statusName。 ie..loop现在应该检查Json元素statusName

1 个答案:

答案 0 :(得分:2)

您可以使用更适合的数组方法,例如Array#filterArray#some,因为您只需要迭代测试,直到找到值并且结果集只是给定的子集阵列。

&#13;
&#13;
ABC_V1
&#13;
&#13;
&#13;