如何测试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);
相关问答
更多-
我一直在把源代码放在你通常期望的地方 - 在'src / main'目录下。 所以,我有src / main / java和src / main javascript,后面的目录中有角度应用程序。 我将角度应用程序设置为src / main / resources / static / ui,并配置SpringBoot应用程序从那里提供服务。 - 附加信息 - 为Angular应用程序的index.html提供服务,我用它: @Bean public WebMvcConfigurerAdapter forw ...
-
Response for preflight is invalid (redirect) 这听起来更像是您的服务器没有正确处理飞行前请求。 你在用CORS吗? (即你使用ng serve来运行你的Angular应用程序,但试图访问不同端口/机器上的服务器?)。 因为,基于您所获得的错误,我认为您的后端服务器未设置为正确处理CORS。 Response for preflight is invalid (redirect) That sounds more like your server isn't ...
-
您无法直接在请求参数上发送JSON。 你需要像在你想传递给你的服务器的json结构上调用encodeURIComponent()那样做,然后让参数成为一个字符串。 在服务器端,您需要将字符串转换回模型对象。 You can't send JSON on the request parameter, directly. You'll need to do something like call encodeURIComponent() on the json structure you want to pas ...
-
angular 5在POST请求中获取错误。(angular 5 Getting an error on POST request. Sending header param not works)[2022-10-10]
尝试这个: 请像这样导入HttpClient和HttpHeaders : import { HttpClient, HttpHeaders } from '@angular/common/http'; 并注入你的班级: constructor(private http: HttpClient) { } 和你的方法是这样的: public getAppData(countryPath):Observable{ let headers: HttpHeaders = new HttpH ... -
@Component @Order(Ordered.HIGHEST_PRECEDENCE) public class CorsFilter implements Filter { public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) { HttpServletResponse response = (HttpServletResponse) res; HttpServletReq ...
-
除非您将文件名作为原始上传中的附加参数或标题传递,否则无法获取文件名。 HTTP不了解或关心文件系统以及与之相关的任何内容。 You cannot get the filename unless you pass it through as an additional parameter or header in the original upload. HTTP does not know or care about filesystems and anything associated with the ...
-
我认为问题在于您如何发送文件。 另一个问题的答案可能会对您有所帮助: https : //stackoverflow.com/a/35669598/5049472 I think the problem is how you send your file. This answer in another question might help you: https://stackoverflow.com/a/35669598/5049472
-
我想你错过了@RequestBody 。 当您使用$http with data属性时,数据将在request body传递。 $http({ method: 'POST', url: url, data: { stateCd: scope.stateCd, addressExtras: scope.addressExtras, uploadFile: scope.upFile }, 我想,一旦你添加@RequestBody ...
-
更改 public ResponseEntity
handleFileUpload(@RequestParam("user") User user, @RequestPart("file") MultipartFile file) 至 public ResponseEntity handleFileUpload(@RequestPart("user") User user, @RequestPart("file") MultipartFile file) 并将请求更改为这样的东西 ... -
将HTML5画布图像上传到服务器Angular2 / Spring Boot(Upload HTML5 canvas image to server Angular2/Spring Boot)[2023-09-18]
事实证明,框架负责在我的画布中渲染内容(Blend4Web)是一个问题。 当我直接从框架提供的方法获取blob对象时,我能够毫无问题地上传文件。 我共享的代码工作正常,因此可以用作参考。 我向那些花时间分析它的人道歉。 It turned out that it is an issue with framework responsible for rendering content in my canvas (Blend4Web). I was able to upload file without any ...