2

I'm trying to pick specific data from my JSON response which looks like this;

{
   "status": "success",
   "reservations": [
      {
         "id": "26630",
         "subject": "Subject",
         "modifiedDate": "2017-05-16T06:05:12",
         "startDate": "2017-05-16T08:00:00",
         "endDate": "2017-05-16T09:45:00",
         "resources": [
            {
               "id": "2408",
               "type": "student_group",
               "code": "groupCode",
               "name": "groupName"
            },
            {
               "id": "3020",
               "type": "realization",
               "code": "realizationCode",
               "name": "realizationName"
            },
            {
               "id": "48",
               "type": "room",
               "code": "roomCode",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "buildingCode",
                  "name": "buildngName"
               },
               "name": "RoomName (PC)"
            }
         ],
         "description": ""
      },
      {
         "id": "21173",
         "subject": "subjectName",
         "modifiedDate": "2017-05-16T06:05:20",
         "startDate": "2017-05-16T08:00:00",
         "endDate": "2017-05-16T16:00:00",
         "resources": [
            {
               "id": "3115",
               "type": "realization",
               "code": "realizationCode",
               "name": "realizationName"
            },
            {
               "id": "2584",
               "type": "student_group",
               "code": "groupCode",
               "name": "groupName"
            },
            {
               "id": "52",
               "type": "room",
               "code": "roomCode",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "buildingCode",
                  "name": "buildingName"
               },
               "name": "roomName (classroom)"
            }
         ],
         "description": ""
      }
   ]
}

I've already used JSON.parse() to make it into an object and went through it with for-loops;

var json = JSON.parse(data.responseText);

for (var i = 0; i < json.reservations.length; i++) {
    if (json.reservations[i].resources != null) {
        for (var j = 0; j < json.reservations[i].resources.length; j++) {

             var reservations = json.reservations[i];
             var resources = json.reservations[i].resources[j];  

         }                              
     }
}

So I would need to pick out the room names before the "description" key name:

"name": "roomName (PC)"
"name": "roomName (classroom)"

I've kept the JSON response a lot shorter for simplicity's sake but usually there's many more of these room names. The idea is to get all the room names from the JSON response body and push them to an array and just printing them out in order like this;

roomName (PC)
roomName (classroom)

Any quick and effective way to do this?

IlariM
  • 346
  • 3
  • 14

4 Answers4

4

You can use such way:

const arrays = json.reservations
  .filter(reservation => reservation.resources)
  .map(reservation =>
    reservation.resources.map(resource => resource.name)
  )
;

const names = [].concat.apply([], arrays);

Array flatten taken from this question: Merge/flatten an array of arrays in JavaScript?

Community
  • 1
  • 1
lunochkin
  • 626
  • 3
  • 16
2

You can first iterate over the json.reservations array with Array.prototype.forEach() and then iterate again over r.resources and make a Array.prototype.push() if the expression: new RegExp(/roomName/, 'i').test(r.name) is satisfied.

Notice that in your json array have lowercase "name": "roomName (classroom)" and uppercase "name": "RoomName (PC)", so the Regular Expression will not check case sensitive with the flag i and finally the RegExp.prototype.test() will check if roomName is in the r.name.

Code:

var json = {"status": "success","reservations": [{"id": "26630","subject": "Subject","modifiedDate": "2017-05-16T06:05:12","startDate": "2017-05-16T08:00:00","endDate": "2017-05-16T09:45:00","resources": [{"id": "2408","type": "student_group","code": "groupCode","name": "groupName"},{"id": "3020","type": "realization","code": "realizationCode","name": "realizationName"},{"id": "48","type": "room","code": "roomCode","parent": {"id": "2","type": "building","code": "buildingCode","name": "buildngName"},"name": "RoomName (PC)"}],"description": ""},{"id": "21173","subject": "subjectName","modifiedDate": "2017-05-16T06:05:20","startDate": "2017-05-16T08:00:00","endDate": "2017-05-16T16:00:00","resources": [{"id": "3115","type": "realization","code": "realizationCode","name": "realizationName"},{"id": "2584","type": "student_group","code": "groupCode","name": "groupName"},{"id": "52","type": "room","code": "roomCode","parent": {"id": "2","type": "building","code": "buildingCode","name": "buildingName"},"name": "roomName (classroom)"}],"description": ""}]},
    result = [],
    regex = new RegExp(/roomName/, 'i');

json.reservations.forEach(function (r) {
  r.resources.forEach(function (r) {
    regex.test(r.name) && result.push({
      name: r.name
    });
  });
})

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Yosvel Quintero Arguelles
  • 15,149
  • 4
  • 33
  • 35
1

You can simply do this:

var a = {
   "status": "success",
   "reservations": [
      {
         "id": "26630",
         "subject": "Subject",
         "modifiedDate": "2017-05-16T06:05:12",
         "startDate": "2017-05-16T08:00:00",
         "endDate": "2017-05-16T09:45:00",
         "resources": [
            {
               "id": "2408",
               "type": "student_group",
               "code": "groupCode",
               "name": "groupName"
            },
            {
               "id": "3020",
               "type": "realization",
               "code": "realizationCode",
               "name": "realizationName"
            },
            {
               "id": "48",
               "type": "room",
               "code": "roomCode",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "buildingCode",
                  "name": "buildngName"
               },
               "name": "RoomName (PC)"
            }
         ],
         "description": ""
      },
      {
         "id": "21173",
         "subject": "subjectName",
         "modifiedDate": "2017-05-16T06:05:20",
         "startDate": "2017-05-16T08:00:00",
         "endDate": "2017-05-16T16:00:00",
         "resources": [
            {
               "id": "3115",
               "type": "realization",
               "code": "realizationCode",
               "name": "realizationName"
            },
            {
               "id": "2584",
               "type": "student_group",
               "code": "groupCode",
               "name": "groupName"
            },
            {
               "id": "52",
               "type": "room",
               "code": "roomCode",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "buildingCode",
                  "name": "buildingName"
               },
               "name": "roomName (classroom)"
            }
         ],
         "description": ""
      }
   ]
}

var b = [];
a.reservations.forEach(function(item){ 
    item.resources.forEach(function(obj){  
        if(obj.type == "room"){
            b.push(obj.name)
        }  
    })  
});
console.log(b); //outputs desired array
Rahul Arora
  • 4,217
  • 1
  • 14
  • 22
0
var jsonStr = "{ \"status\": \"success\", \"reservations\": [ { \"id\": \"26630\", \"subject\": \"Subject\", \"modifiedDate\": \"2017-05-16T06:05:12\", \"startDate\": \"2017-05-16T08:00:00\", \"endDate\": \"2017-05-16T09:45:00\", \"resources\": [ { \"id\": \"2408\", \"type\": \"student_group\", \"code\": \"groupCode\", \"name\": \"groupName\" }, { \"id\": \"3020\", \"type\": \"realization\", \"code\": \"realizationCode\", \"name\": \"realizationName\" }, { \"id\": \"48\", \"type\": \"room\", \"code\": \"roomCode\", \"parent\": { \"id\": \"2\", \"type\": \"building\", \"code\": \"buildingCode\", \"name\": \"buildngName\" }, \"name\": \"RoomName (PC)\" } ], \"description\": \"\" }, { \"id\": \"21173\", \"subject\": \"subjectName\", \"modifiedDate\": \"2017-05-16T06:05:20\", \"startDate\": \"2017-05-16T08:00:00\", \"endDate\": \"2017-05-16T16:00:00\", \"resources\": [ { \"id\": \"3115\", \"type\": \"realization\", \"code\": \"realizationCode\", \"name\": \"realizationName\" }, { \"id\": \"2584\", \"type\": \"student_group\", \"code\": \"groupCode\", \"name\": \"groupName\" }, { \"id\": \"52\", \"type\": \"room\", \"code\": \"roomCode\", \"parent\": { \"id\": \"2\", \"type\": \"building\", \"code\": \"buildingCode\", \"name\": \"buildingName\" }, \"name\": \"roomName (classroom)\" } ], \"description\": \"\" } ] }";
var json = JSON.parse(jsonStr);
var array = [];  
for (var i = 0; i < json.reservations.length; i++) {
    if (json.reservations[i].resources != null) {

        for (var j = 0; j < json.reservations[i].resources.length; j++) {
             var resource = json.reservations[i].resources[j];
             if (resource.type === "room") {
                 if (array.indexOf("code")) {
                     array.push(resource.name);
                 }                                     
             }                                                                                         
         }

     }
}
 console.log(array);

output in console. Please check..

(2) ["RoomName (PC)", "roomName (classroom)"]0: "RoomName (PC)"1: "roomName (classroom)"length: 2__proto__: Array(0)