Javascript计数对象的密钥有点表示法

时间:2017-12-09 08:23:25

标签: javascript jquery arrays object laravel-5.5

尝试计算javascript对象中的数组。此响应来自laravel验证器

  {  
   "message":"The given data was invalid.",
   "errors":{  
      "roomtype_id":[  
         "Please select a room type"
      ],
      "price":[  
         "Price cannot be empty"
      ],
      "amenities":[  
         "Atleast select one amenities"
      ],
      "floor.0":[  
         "Please select a floor"
      ],
      "floor.1":[  
         "Please select a floor"
      ],
      "floor.2":[  
         "Please select a floor"
      ],
      "floor.3":[  
         "Please select a floor"
      ],
      "floor.4":[  
         "Please select a floor"
      ],
      "room_number.0":[  
         "Room number cannot be empty"
      ],
      "room_number.1":[  
         "Room number cannot be empty"
      ],
      "room_number.2":[  
         "Room number cannot be empty"
      ],
      "room_number.3":[  
         "Room number cannot be empty"
      ],
      "room_number.4":[  
         "Room number cannot be empty"
      ],
      "room_number.5":[  
         "Room number cannot be empty"
      ],
      "room_number.6":[  
         "Room number cannot be empty"
      ],
      "room_number.7":[  
         "Room number cannot be empty"
      ],
      "room_number.8":[  
         "Room number cannot be empty"
      ],
      "room_number.9":[  
         "Room number cannot be empty"
      ],
      "room_number.10":[  
         "Room number cannot be empty"
      ],
      "room_number.11":[  
         "Room number cannot be empty"
      ],
      "room_number.12":[  
         "Room number cannot be empty"
      ],
      "room_number.13":[  
         "Room number cannot be empty"
      ],
      "room_number.14":[  
         "Room number cannot be empty"
      ],
      "room_number.15":[  
         "Room number cannot be empty"
      ],
      "room_number.16":[  
         "Room number cannot be empty"
      ],
      "room_number.17":[  
         "Room number cannot be empty"
      ],
      "room_number.18":[  
         "Room number cannot be empty"
      ],
      "room_number.19":[  
         "Room number cannot be empty"
      ],
      "room_number.20":[  
         "Room number cannot be empty"
      ],
      "room_number.21":[  
         "Room number cannot be empty"
      ],
      "room_number.22":[  
         "Room number cannot be empty"
      ],
      "room_number.23":[  
         "Room number cannot be empty"
      ],
      "room_number.24":[  
         "Room number cannot be empty"
      ],
      "room_number.25":[  
         "Room number cannot be empty"
      ],
      "room_number.26":[  
         "Room number cannot be empty"
      ],
      "room_number.27":[  
         "Room number cannot be empty"
      ],
      "room_number.28":[  
         "Room number cannot be empty"
      ]
   }
}

在上面的js对象中,错误对象包含"floor.0":["Please select a floor"],"floor.1":["Please select a floor"],"floor.2":["Please select a floor"]等等。

我想计算出现了多少个floor.*数组

注意: 我正在访问单个值,如下面的

$("#floor_error_1").text(data.responseJSON.errors["floor.0"]);
$("#floor_error_2").text(data.responseJSON.errors["floor.1"]);

怎么算?并循环并打印错误?

2 个答案:

答案 0 :(得分:1)

您可以使用Object.keys()获取数组中keys的{​​{1}}。然后error只有startsWith" floor"并采取它的长度。

您也可以使用正则表达式而不是filter

startsWith

要打印错误,您可以尝试:

Object.keys(data.errors).filter(x => x.startsWith('floor')).length



let errorKeys = Object.keys(data.errors).filter(x => x.startsWith('floor'));

let count = errorKeys.length;

let floorErrors = errorKeys.map(key => ({
    key: key,
    errors: data.errors[key]
}));




答案 1 :(得分:1)

您可以通过循环计数来执行此操作:

    var data = {};
	data.responseJSON = '{"message":"The given data was invalid.","errors":{"roomtype_id":["Please select a room type"],"price":["Price cannot be empty"],"amenities":["Atleast select one amenities"],"floor.0":["Please select a floor"],"floor.1":["Please select a floor"],"floor.2":["Please select a floor"],"floor.3":["Please select a floor"],"floor.4":["Please select a floor"],"room_number.0":["Room number cannot be empty"],"room_number.1":["Room number cannot be empty"],"room_number.2":["Room number cannot be empty"],"room_number.3":["Room number cannot be empty"],"room_number.4":["Room number cannot be empty"],"room_number.5":["Room number cannot be empty"],"room_number.6":["Room number cannot be empty"],"room_number.7":["Room number cannot be empty"],"room_number.8":["Room number cannot be empty"],"room_number.9":["Room number cannot be empty"],"room_number.10":["Room number cannot be empty"],"room_number.11":["Room number cannot be empty"],"room_number.12":["Room number cannot be empty"],"room_number.13":["Room number cannot be empty"],"room_number.14":["Room number cannot be empty"],"room_number.15":["Room number cannot be empty"],"room_number.16":["Room number cannot be empty"],"room_number.17":["Room number cannot be empty"],"room_number.18":["Room number cannot be empty"],"room_number.19":["Room number cannot be empty"],"room_number.20":["Room number cannot be empty"],"room_number.21":["Room number cannot be empty"],"room_number.22":["Room number cannot be empty"],"room_number.23":["Room number cannot be empty"],"room_number.24":["Room number cannot be empty"],"room_number.25":["Room number cannot be empty"],"room_number.26":["Room number cannot be empty"],"room_number.27":["Room number cannot be empty"],"room_number.28":["Room number cannot be empty"]}}';
	data.responseJSON = JSON.parse( data.responseJSON );

        //Start of looping here
	var counter = 0;
	for ( var key in data.responseJSON.errors ) {
                //Check if 'floor' is found on kkey
		if ( key.indexOf( "floor." ) !== -1 ) {
			//the key has text floor.
			counter++;
			console.log( data.responseJSON.errors[ key ][0] );
		} 
	}
	
	console.log( "Total error is " + counter );

这将显示楼层的所有错误并计算它。一石二鸟。