Abhishek Asthana Abhishek Asthana - 5 months ago 13
JSON Question

using bracket notation for reading json in javascript

I have a requirement where I have to read the value of the JSON data for the column which user passes

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
will be passed by the user in a function
but when ever I try to log the value of
arr[k][columnName]
, it only gives me the very first value of the array.
What is the possible reason

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 Array

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


result 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 is a variable, you can understand it as a simple variable holding name of the column
ex:
statusName


So this should work like, the condition will check for the statusName only.
ie..loop should now check Json element statusName

Answer

You could use a bit more suited array methods, like Array#filter and Array#some, because you need only iterate for the test until a value is found and the result set is just a subset of the given array.

var arr = [{ 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" }],
    checkedFilters = ["Open", "Unset", "Closed"],
    columnName = 'statusName',
    temporaryFilterData = arr.filter(function (a) {
        return checkedFilters.some(function (b) {
            return a[columnName] == b;
        });
    });

console.log(temporaryFilterData);