繁体   English   中英

与这个过滤器函数相比有什么问题吗,因为它根本不返回任何东西

[英]Is anything wrong comparing with this filter function cause it isn't returning anything at all

我正在尝试过滤全日历上的某些事件,但无法获得想要的适当对象。 我正在使用函数过滤器,如下代码:

我尝试过先使用===然后再使用==因为我希望这些值相同。 我什至尝试使用&&来验证是否应该使用单个String而不是array

 document.addEventListener('DOMContentLoaded', function () {
        $.ajax({
            type: 'GET',
            url: "connection with API",
            success: function (doc) {
                        listData = doc;
                        console.log("done");
                        $(doc).each(function (idata) {
                            var dt = new Date(doc[idata].EDA);
                            dt.setHours(dt.getHours() + 1);
                            var dt2 = new Date(doc[idata].EDA);
                            dt2.setMinutes(dt2.getMinutes() + 30);
                            var dt3 = new Date(doc[idata].EDA);
                            dt3.setMinutes(dt3.getMinutes() + 30);
                            if (doc[idata].REFERENCE_NUMBER.split("/")[1] == "R9" && doc[idata].CARRIER_CODE_FOR_DISPLAY == "SESE") {
                                allEvents.push({
                                    id: 1,
                                    resourceId: '2',
                                    title: doc[idata].CARRIER_CODE_FOR_DISPLAY + " - " + doc[idata].TRAILER_ID.split("/")[1],
                                    start: doc[idata].EDA,
                                    end: dt,
                                    allday: false,
                                    backgroundColor: '#041E42',
                                    textColor: '#FAFAFA',
                                });
                            }
                            if (doc[idata].REFERENCE_NUMBER.split("/")[1] == "B" && doc[idata].CARRIER_CODE_FOR_DISPLAY == "SESE") {
                                allEvents.push({
                                    id: 2,
                                    resourceIds: ['1','3', '4'],
                                    title: doc[idata].CARRIER_CODE_FOR_DISPLAY + " - " + doc[idata].TRAILER_ID.split("/")[1],
                                    start: doc[idata].EDA,
                                    end: dt3,
                                    allday: false,
                                    backgroundColor: '#041E42',
                                    textColor: '#FAFAFA',
                                });

                            }
                            if (doc[idata].REFERENCE_NUMBER.split("/")[1] == "C" && doc[idata].CARRIER_CODE_FOR_DISPLAY == "SESE") {
                                allEvents.push({
                                    id: 2,
                                    resourceIds: ['3','4'],
                                    title: doc[idata].CARRIER_CODE_FOR_DISPLAY + " - " + doc[idata].TRAILER_ID.split("/")[1],
                                    start: doc[idata].EDA,
                                    end: dt2,
                                    allday: false,
                                    backgroundColor: '#041E42',
                                    textColor: '#FAFAFA',
                                });

                            }

                            if (doc[idata].REFERENCE_NUMBER.split("/")[1] == "R4" && doc[idata].CARRIER_CODE_FOR_DISPLAY== "SESE") {
                                allEvents.push({
                                    id: 2,
                                    resourceId: '1',
                                    title: doc[idata].CARRIER_CODE_FOR_DISPLAY + " - " + doc[idata].TRAILER_ID.split("/")[1],
                                    start: doc[idata].EDA,
                                    end: dt,
                                    allday: false,
                                    backgroundColor: '#041E42',
                                    textColor: '#FAFAFA',
                                });

                                }
                            if (doc[idata].REFERENCE_NUMBER.split("/")[1] == "KD") {
                                allEvents.push({
                                    id: 3,
                                    resourceId: '6',
                                    title: doc[idata].CARRIER_CODE_FOR_DISPLAY,
                                    start: doc[idata].EDA,
                                    end: dt,
                                    allday: false,
                                    backgroundColor: '#94A596',
                                    textColor: '#FAFAFA',
                                    height: ''

                                });
                            }

                        }); 
let eventus3 = allEvents.filter((evento) => {
                    return evento.resourceIds == ['3','4'];
                })

您不能以这种方式比较数组:

evento.resourceIds == ['3', '4'];

对象(包括数组)通过引用进行比较,即==(或===)仅在它们指向同一对象时才返回true。 请注意, [1,2]==[1,2]为假。

模拟数组==的一种方法是比较它们的JSON表示,因此在此示例中

JSON.stringify(evento.resourceIds) === JSON.stringify(['3', '4']);

(不确定这是最好的 ,但可能是最简洁的)。

数组指针做数据,当你做这样的事情['1', '2'] == ['1', '2']你在内存比较两个型动物的指针,因此,你将获得false ,即使内容是一样的。 如果要比较两个数组,则应遍历它们,看看每个元素是否等于另一个数组中的元素。

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM