2014-11-19 4 views
1

아래 JSON 데이터가 있습니다. 난 당신이 이름 섹션과 그 값을 가진 키가 내가 키 값 쌍 이 필드 배열에 관찰 할 경우 위의 JSON 데이터에서 부서 및 직원키 값을 기준으로 json을 필터링하십시오.

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" 
     } 



    ] 


}; 

을 기반으로 키 값 쌍을 기준으로 필터링 할 .

저는 모든 직원과 모든 부서를 나열하고 싶은 것처럼 섹션을 기준으로 데이터를 필터링하고 싶습니다. 반복에 대해 혼란스러워합니다. 이걸 좀 도와 주 시겠어요?

내가 원하는 두 개체 ANS 출력 VAR의 한 EMP = [] VAR의 deps = [] 난 반복하고 EMP [ "명칭"] EMP [ "이름"] 등에 액세스 할 수 있도록

DEP [ " DepName "] 등

+0

그래서 이것을 병합하고 키 - 값 쌍 목록을 얻으시겠습니까? – filur

+0

예상되는 출력을 게시 할 수 있습니까? – BatScream

+0

예 섹션을 기반으로 키 값 쌍을 가져 오려면 모든 직원이 하나의 배열로 이동해야하며 모든 부서가 다른 배열로 이동해야합니다. – user804401

답변

1

한 가지 방법 :

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); 

오/피 :

{ 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' } ] } 

당신은 result.Department[0].Titleresult.Employee[i].Title 및 부서로 각 직원에 액세스 할 수 있습니다.

+0

코드가 제대로 작동하지만 Hardware, HouseKeeping 같은 여러 섹션에 여러 조건이있을 경우 어떻게됩니까? 어쨌든 우리가 이것을 리펙토링 할 수 있습니까? – user804401

+0

업데이트 된 답변보기. – BatScream

1

이 당신에게 아이디어를 제공해야합니다 :

var firstFields = data.Item[0].Fields.map(function(field){ 
    return { key: field.Key, value: field.Value }; 
}); 

console.log(firstFields); 

map() 데이터를 당신이 선호하는 구조를 만들 수 있습니다.

직원과 부서를 구별하는 방법에 대한 예제가 업데이트됩니다.

http://jsfiddle.net/kwpy35hg/

업데이트 :

// 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/

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; 
}); 
+0

안녕하세요, 코드가 두 부서를 모두 포함하므로 두 부서의 배열로 올 수 있습니다. – user804401

+0

@ user804401이 코드를 기반으로 계산할 수 있어야합니다.) 어쨌든 예제를 게시했습니다 – filur

관련 문제