Reputation: 1994
I have the below JSON data. I want to filter based on Key value pairs that is based on Department and Employee
var data = {
"Item": [
{
"Fields": [
{
"Key": "Title",
"Value": ""
},
{
"Key":"Name",
"Value":"Phani"
},
{
"Key":"Designation",
"Value":"Software Eng"
},
{
"Key":"Salary",
"Value":""
},
{
**"Key":"Section",**
"Value":"Employee"
}], "ItemName": "Test"
},
{
"Fields": [
{
"Key": "Title",
"Value": ""
},
{
"Key": "Name",
"Value": "Raju"
},
{
"Key": "Designation",
"Value": "Software Eng"
},
{
"Key": "Salary",
"Value": ""
},
{
"Key": "Section",
"Value": "Employee"
}], "ItemName": "Test"
},
{
"Fields": [
{
"Key": "Title",
"Value": ""
},
{
"Key": "DepName",
"Value": "Finance"
},
{
"Key": "DepType",
"Value": "Money"
},
{
"Key": "Salary",
"Value": ""
},
{
"Key": "Section",
"Value": "Department"
}], "ItemName": "Test"
},
{
"Fields": [
{
"Key": "Title",
"Value": ""
},
{
"Key": "DepName",
"Value": "IT"
},
{
"Key": "DepType",
"Value": "Tech"
},
{
"Key": "Salary",
"Value": ""
},
{
"Key": "Section",
"Value": "Department"
}], "ItemName": "Test"
}
]
};
In the above json data if you observe in Fields array i have key value pairs There is a key with name Section and its value.
I want to filter the data based on Section, like i want to list all the employees and all the departments. Confused about iterating. could you please help with this.
I want two objects ans output var emps =[] var deps=[]
so that i can iterate and access like emp["Designation"] emp["Name"] dep["DepName"] etc
Upvotes: 2
Views: 10246
Reputation: 19700
One way of doing it would be:
var result = {};
data.Item.forEach(function(item){
var fields = item.Fields;
var res = {};
for(var i=0;i<fields.length;i++)
{
res[fields[i].Key] = fields[i].Value;
}
if(!result.hasOwnProperty(res.Section)){
result[res.Section] = [];
}
result[res.Section].push(res);
})
console.log(result);
o/p:
{ Employee:
[ { Title: '',
Name: 'Phani',
Designation: 'Software Eng',
Salary: '',
Section: 'Employee' },
{ Title: '',
Name: 'Raju',
Designation: 'Software Eng',
Salary: '',
Section: 'Employee' } ],
Department:
[ { Title: '',
DepName: 'Finance',
DepType: 'Money',
Salary: '',
Section: 'Department' },
{ Title: '',
DepName: 'IT',
DepType: 'Tech',
Salary: '',
Section: 'Department' } ] }
You can access each employee as result.Employee[i].Title
and department as result.Department[0].Title
.
Upvotes: 1
Reputation: 1576
This should give you an idea:
var firstFields = data.Item[0].Fields.map(function(field){
return { key: field.Key, value: field.Value };
});
console.log(firstFields);
map()
the data and create the structure that you prefer.
I will update with an example on how to differentiate between employees and departments.
Update:
// filter out departments
var departments = data.Item.filter(function(item){
var section = item.Fields[item.Fields.length -1];
return section.Value === 'Department';
});
// flatten result:
deps = [];
for(var i = 0; i < departments.length; i++){
for(var j = 0; j < departments[i].Fields.length; j++){
deps.push({ key: departments[i].Fields[j].Key,
value: departments[i].Fields[j].Value })
}
}
console.dir(deps);
http://jsfiddle.net/kwpy35hg/1/
To get departments as 2 arrays:
var departments = data.Item.filter(function(item){
var section = item.Fields[item.Fields.length -1];
return section.Value === 'Department';
}).map(function(deps){
return deps.Fields;
});
Upvotes: 1