首页 \ 问答 \ 如何测试Javascript数组中是否存在对象?(How do I test if an object exists in a Javascript array?)

如何测试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
更新时间:2022-02-18 14:02

最满意答案

更改

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 only MediaType.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);

相关问答

更多

相关文章

更多

最新问答

更多
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • Java中的不可变类(Immutable class in Java)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • EXCEL VBA 基础教程下载
  • RoR - 邮件中的动态主体(部分)(RoR - Dynamic body (part) in mailer)
  • 无法在Google Script中返回2D数组?(Can not return 2D Array in Google Script?)
  • JAVA环境变量的设置和对path , classpth ,java_home设置作用和目的?
  • mysql 关于分组查询、时间条件查询
  • 如何使用PowerShell匹配运算符(How to use the PowerShell match operator)
  • Effective C ++,第三版:重载const函数(Effective C++, Third edition: Overloading const function)
  • 如何用DELPHI动态建立MYSQL的数据库和表? 请示出源代码。谢谢!
  • 带有简单redis应用程序的Node.js抛出“未处理的错误”(Node.js with simple redis application throwing 'unhandled error')
  • 使用前端框架带来哪些好处,相对于使用jquery
  • Ruby将字符串($ 100.99)转换为float或BigDecimal(Ruby convert string ($100.99) to float or BigDecimal)
  • 高考完可以去做些什么?注意什么?
  • 如何声明放在main之后的类模板?(How do I declare a class template that is placed after the main?)
  • 如何使用XSLT基于兄弟姐妹对元素进行分组(How to group elements based on their siblings using XSLT)
  • 在wordpress中的所有页面的标志(Logo in all pages in wordpress)
  • R:使用rollapply对列组进行求和的问题(R: Problems using rollapply to sum groups of columns)
  • Allauth不会保存其他字段(Allauth will not save additional fields)
  • python中使用sys模块中sys.exit()好像不能退出?
  • 将Int拆分为3个字节并返回C语言(Splitting an Int to 3 bytes and back in C)
  • 在SD / MMC中启用DDR会导致问题吗?(Enabling DDR in SD/MMC causes problems? CMD 11 gives a response but the voltage switch wont complete)
  • sed没有按预期工作,从字符串中间删除特殊字符(sed not working as expected, removing special character from middle of string)
  • 如何将字符串转换为Elixir中的函数(how to convert a string to a function in Elixir)