I have the below JSON data. I want to filter based on Key value pairs that is based on Department and Employee
我有以下JSON数据。我想根据基于Department和Employee的Key值对进行过滤
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.
在上面的json数据中,如果你在Fields数组中观察到我有键值对有一个名为Section的键及其值。
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.
我想根据Section过滤数据,比如我想列出所有员工和所有部门。对迭代感到困惑。你能帮忙吗?
I want two objects ans output var emps =[] var deps=[]
我想要两个对象ans输出var emps = [] var deps = []
so that i can iterate and access like emp["Designation"] emp["Name"] dep["DepName"] etc
所以我可以迭代和访问像emp [“指定”] emp [“名称”] dep [“DepName”]等
2 个解决方案
#1
1
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
.
您可以访问每个员工作为结果。员工[i]。标题和部门作为结果。部门[0]。标题。
#2
1
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.
map()数据并创建您喜欢的结构。
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:
将部门作为2个数组:
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;
});
#1
1
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
.
您可以访问每个员工作为结果。员工[i]。标题和部门作为结果。部门[0]。标题。
#2
1
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.
map()数据并创建您喜欢的结构。
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:
将部门作为2个数组:
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;
});