JavaScript - 从 JSON 对象中选取特定数据



我正在尝试从我的 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": ""
      }
   ]
}

我已经用JSON.parse()把它做成一个对象,并用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];  
         }                              
     }
}

所以我需要在"description"键名称之前挑选房间名称:

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

为了简单起见,我将 JSON 响应缩短了很多,但通常还有更多这样的房间名称。这个想法是从 JSON 响应正文中获取所有房间名称并将它们推送到数组中,然后按这样的顺序打印出来;

roomName (PC)
roomName (classroom)

有什么快速有效的方法可以做到这一点吗?

你可以这样使用:

const arrays = json.reservations
  .filter(reservation => reservation.resources)
  .map(reservation =>
    reservation.resources.map(resource => resource.name)
  )
;
const names = [].concat.apply([], arrays);

数组扁平化

取自这个问题:在 JavaScript 中合并/扁平化数组数组?

你可以

先用 Array.prototype.forEach(( 遍历json.reservations数组,然后再次迭代r.resources,如果表达式满足 new RegExp(/roomName/, 'i').test(r.name),则创建一个 Array.prototype.push((。

请注意,在您的json数组中具有小写"name": "roomName (classroom)"和大写"name": "RoomName (PC)",因此正则表达式不会使用标志i检查区分大小写,最后RegExp.prototype.test((将检查roomName是否在r.name中。

法典:

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

你可以简单地这样做:

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

在控制台中输出。请检查..

(2( [

"房间名称 (PC(", "房间名称(教室("]0: "房间名称 (PC("1: "房间名称(教室("长度: 2__proto__: 数组(0(

最新更新