简体   繁体   English

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

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

I'm trying to filter some events on my fullcalendar but I can't get the proper objects that I want to. 我正在尝试过滤全日历上的某些事件,但无法获得想要的适当对象。 I'm using the function filter, as the following code: 我正在使用函数过滤器,如下代码:

I have tried using === first then i used == because I want the values to be the same. 我尝试过先使用===然后再使用==因为我希望这些值相同。 I even tried using && to verify if I should use single Strings instead of array 我什至尝试使用&&来验证是否应该使用单个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'];
                })

You can't compare arrays this way: 您不能以这种方式比较数组:

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

Objects (arrays included) are compared by reference, that is, == (or ===) returns true only if they point at the same object. 对象(包括数组)通过引用进行比较,即==(或===)仅在它们指向同一对象时才返回true。 Notice that [1,2]==[1,2] is false. 请注意, [1,2]==[1,2]为假。

One way to simulate a == for arrays would be to compare their JSON representations, so in this example 模拟数组==的一种方法是比较它们的JSON表示,因此在此示例中

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

(Not sure if it's the best , but probably the most concise). (不确定这是最好的 ,但可能是最简洁的)。

Arrays are pointers do data, when you do something like ['1', '2'] == ['1', '2'] , you are comparing two differents pointers in the memory, therefore you will obtain false , even though the content is the same. 数组指针做数据,当你做这样的事情['1', '2'] == ['1', '2']你在内存比较两个型动物的指针,因此,你将获得false ,即使内容是一样的。 If you want to compare two arrays, you should loop through them and see if each element is equal to the element in the other array. 如果要比较两个数组,则应遍历它们,看看每个元素是否等于另一个数组中的元素。

Hope this helps. 希望这可以帮助。

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

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