最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - filter json based on key value - Stack Overflow

matteradmin4PV0评论

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

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

Share Improve this question edited Nov 19, 2014 at 7:13 user804401 asked Nov 19, 2014 at 7:02 user804401user804401 1,9949 gold badges43 silver badges75 bronze badges 3
  • So you want to flatten this and get a list of key-value pairs? – filur Commented Nov 19, 2014 at 7:06
  • Can you please post your expected output? – BatScream Commented Nov 19, 2014 at 7:08
  • Yes i want to get key value pairs based on section, all the employees should go into one array and all the Departments should go into another array – user804401 Commented Nov 19, 2014 at 7:08
Add a ment  | 

2 Answers 2

Reset to default 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.

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.

http://jsfiddle/kwpy35hg/

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/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;
});
Post a comment

comment list (0)

  1. No comments so far