如何测试Javascript数组中是否存在对象?(How do I test if an object exists in a Javascript array?)
我已经看到了其他问题,我已经尝试了过滤功能,但我不确定我是否正确使用它。
基本上我有一个对象数组,如下所示:
[{"hour":"6 am", "date":"2012-12-01"},{"hour":"7 am", "date":"2012-12-01"}]
我循环了一段时间,每天我都会在早上6点到晚上9点之间循环。 如果上述数组中存在小时(如该特定日期的小时)。 我想将它标记为可用于新对象,然后传递给新数组。 以下是我目前使用的代码。
for(var i = 0; i < dayCount; i++){//Loop through the days that exist in the schedule day = new Object(); day.date = Date.parse(startDate).add(i).days(); day.dayName = weekday[day.date.getDay()] day.hours = new Array(); for(var j = 6; j < 22; j++){ //Loop through hours of the day seeing if they're available/scheduled, etc. if(j<=12){ thisHour = j +' am'; } else{ thisHour = j-12 + ' pm'; } var thisIsAvailable = $(assignedHours).filter(function(){ return assignedHours.hour == thisHour && assignedHours.date == day.date.toString("yyyy-MM-dd"); }); var thisIsScheduled = 0; day.hours.push({hour: thisHour,available: thisIsAvailable, scheduled: thisIsScheduled}); } daysInSchedule.push(day); }
几个笔记。 我在day.date属性上使用.toString(),因为它是以JS Date格式格式化的,我正在比较它的值是MYSQL Date格式。 我已经提醒(谈论旧学校调试)thisIsAvailable.length,我每次都得到0。 任何想法都表示赞赏。 谢谢!
编辑:刚刚意识到我忘了告诉你一切非常重要的事情。 我给你的数组包含在变量Named assignedHours中。 很抱歉离开了。
编辑2:为了澄清,我的问题是在两个代码摘录之间的位置。 我试图看看给定数组中的一个对象是否匹配循环中的日期和小时,以及我正在运行的嵌套循环。 如果确实如此,我想将其传递给一个新对象,然后我将其推入当天的小时数组中。 如果没有,那么我仍然传递对象但是为0可用。
I've seen other questions, and I've tried the filter function, but I'm not sure if I'm using it correctly.
Essentially I have an array of objects that looks like this:
[{"hour":"6 am", "date":"2012-12-01"},{"hour":"7 am", "date":"2012-12-01"}]
I'm looping through a set period of days, and on each day I'm looping through the hours between 6am and 9pm. If the hour(as in the hour on that specific date) exists in the above array. I want to mark it as available in a new object that I then pass to a new array. Below is the code I'm currently using.
for(var i = 0; i < dayCount; i++){//Loop through the days that exist in the schedule day = new Object(); day.date = Date.parse(startDate).add(i).days(); day.dayName = weekday[day.date.getDay()] day.hours = new Array(); for(var j = 6; j < 22; j++){ //Loop through hours of the day seeing if they're available/scheduled, etc. if(j<=12){ thisHour = j +' am'; } else{ thisHour = j-12 + ' pm'; } var thisIsAvailable = $(assignedHours).filter(function(){ return assignedHours.hour == thisHour && assignedHours.date == day.date.toString("yyyy-MM-dd"); }); var thisIsScheduled = 0; day.hours.push({hour: thisHour,available: thisIsAvailable, scheduled: thisIsScheduled}); } daysInSchedule.push(day); }
A couple notes. I use .toString() on the day.date property because it is formatted in JS Date format and the value I'm comparing it against is in the MYSQL Date format. I've alerted (talk about old school debugging) thisIsAvailable.length, and I get 0 everytime. Any ideas are appreciated. Thanks!
EDIT: Just realized I forgot to tell you all something very important. The array I give you is contained in the variable Named assignedHours. Sorry about leaving that out.
EDIT 2: To clarify, my question is in the bit between the two code excerpts. I'm trying to see if one of the objects in the given array matches the day and hour in the loop, and nested loop I'm running through. If it does I want to pass that on to a new object which I then push into the hours array of the day. If not then I still pass the object but with a 0 value for it being available.
原文:https://stackoverflow.com/questions/13179038
满意答案
更改
public ResponseEntity<User> handleFileUpload(@RequestParam("user") User user, @RequestPart("file") MultipartFile file)
至
public ResponseEntity<User> handleFileUpload(@RequestPart("user") User user, @RequestPart("file") MultipartFile file)
并将请求更改为这样的东西将起作用:
curl -i -X POST -H "Content-Type: multipart/form-data" \ -F 'user={"name":"John","age":12};type=application/json' \ -F "file=@myfile.txt" http://localhost:8080/post
仅供
consumes
使用MediaType.MULTIPART_FORM_DATA_VALUE
。为了在角度上提出上述类型的请求,可以这样做:
const userBlob = new Blob(JSON.stringify(new User('John', 12)),{ type: "application/json"}); formdata.append('user', userBlob);
Changing
public ResponseEntity<User> handleFileUpload(@RequestParam("user") User user, @RequestPart("file") MultipartFile file)
to
public ResponseEntity<User> handleFileUpload(@RequestPart("user") User user, @RequestPart("file") MultipartFile file)
and changing the request to something like this will work:
curl -i -X POST -H "Content-Type: multipart/form-data" \ -F 'user={"name":"John","age":12};type=application/json' \ -F "file=@myfile.txt" http://localhost:8080/post
For
consumes
onlyMediaType.MULTIPART_FORM_DATA_VALUE
is required.To make above kind of request in angular, something like this can be done:
const userBlob = new Blob(JSON.stringify(new User('John', 12)),{ type: "application/json"}); formdata.append('user', userBlob);
相关问答
更多Spring Boot和Angular(5)项目结构(Spring Boot and Angular (5) project structure)
Spring Boot + Angular 2 + JWT(Spring Boot + Angular 2 + JWT)
Spring Boot:如何将JSON对象添加到GET请求?(Spring Boot: How to add JSON Object to GET request?)
angular 5在POST请求中获取错误。(angular 5 Getting an error on POST request. Sending header param not works)
通过Angular 2和JSON发送对象到Spring-Boot的RestController(Sending objects to Spring-Boot's RestController Via Angular 2 and JSON)
Http请求。(Http Request. How to get png name? Spring Boot)
Angular 2 post请求不发送图像数据(Angular 2 post request not send image data)
Angular http和spring boot rest服务(Angular http and spring boot rest service)
使用单个请求发送JSON和图像。(Send JSON and Image with single request. Angular + Spring Boot)
将HTML5画布图像上传到服务器Angular2 / Spring Boot(Upload HTML5 canvas image to server Angular2/Spring Boot)
相关文章
更多Object Oriented Programming
javascript常用对象
javascript快速入门
javascript 问题
JavaScript 事件
JavaScript 事件
Guava学习笔记:复写的Object常用方法
在main方法中开启线程与@Test中开启线程
请教javascript问题
Hadoop 0.21如何运行单个test case
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!