如何使用数组值过滤对象?

抱歉英语不好:/


你好朋友,我有这样一个对象。我通过一个按钮将某些值加载到一个系列中。我需要用这个数组的元素过滤这个对象。我怎样才能做到这一点?


数组中的每个元素都应该查询 wtype_id。如果没有,它不应该显示在屏幕上。


同时,该对象拥有 CITY - DISTRICT - SERVICE - LOCATION 系统。我不得不质疑服务。此外,还有一个 Get 中包含多个 City 和 District 的状态。


这是我的数组:


0: "1"

1: "2"

2: "3"

3: "4"

4: "5"

5: "6"

6: "7"

这是我的对象


{

    "41": {

        "citys_id": "41",

        "citys_name": "Kocaeli",

        "sub_citys": {

            "505": {

                "subcity_id": "505",

                "subcity_name": "İzmit",

                "subcity_city_id": "41",

                "wtype": {

                    "1": {

                        "wtype_id": "1",

                        "wtype_name": "Yemek",

                        "wtype_icon": "http://icons.iconarchive.com/icons/dtafalonso/android-lollipop/32/Phone-icon.png",

                        "locations": [

                            {

                                "loc_id": "13",

                                "loc_type": "1",

                                "loc_name": "With Beyond Bilişim",

                                "loc_number": "05322510292",

                                "loc_pazartesi": "1",

                                "loc_sali": "1",

                                "loc_carsamba": "1",

                                "loc_persembe": "1",

                                "loc_cuma": "1",

                                "loc_cumartesi": "0",

                                "loc_pazar": "1",

                                "loc_open": "1100",

                                "loc_close": "1400",

                                "loc_city": "41",

                                "loc_subcity": "505",

                                "loc_adress": "Çukurbağ Mahallesi, Molla Raşit Sokak, Numara: 14",

                                "loc_tarif": "Oradan çıkınca sola doğru dönüp devam edeceğiz",

                                "loc_lat": "41.0710419",

                                "loc_lng": "29.0127192"

                            }

                        ]

                    },

        }

    }

}


qq_遁去的一_1
浏览 90回答 1
1回答

噜噜哒

好的,让我们用一个交互式代码片段来试试吧:var props = {&nbsp; "41": {&nbsp; &nbsp; "citys_id": "41",&nbsp; &nbsp; "citys_name": "Kocaeli",&nbsp; &nbsp; "sub_citys": {&nbsp; &nbsp; &nbsp; "505": {&nbsp; &nbsp; &nbsp; &nbsp; "subcity_id": "505",&nbsp; &nbsp; &nbsp; &nbsp; "subcity_name": "İzmit",&nbsp; &nbsp; &nbsp; &nbsp; "subcity_city_id": "41",&nbsp; &nbsp; &nbsp; &nbsp; "wtype": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "1": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "wtype_id": "1",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "wtype_name": "Yemek",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "wtype_icon": "http://icons.iconarchive.com/icons/dtafalonso/android-lollipop/32/Phone-icon.png",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "locations": [{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_id": "13",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_type": "1",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_name": "With Beyond Bilişim",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_number": "05322510292",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_pazartesi": "1",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_sali": "1",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_carsamba": "1",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_persembe": "1",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_cuma": "1",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_cumartesi": "0",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_pazar": "1",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_open": "1100",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_close": "1400",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_city": "41",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_subcity": "505",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_adress": "Çukurbağ Mahallesi, Molla Raşit Sokak, Numara: 14",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_tarif": "Oradan çıkınca sola doğru dönüp devam edeceğiz",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_lat": "41.0710419",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_lng": "29.0127192"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "3": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "wtype_id": "3",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "wtype_name": "Eczane",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "wtype_icon": "https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "locations": [{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_id": "14",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_type": "3",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_name": "a",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_number": "b",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_pazartesi": "0",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_sali": "0",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_carsamba": "0",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_persembe": "0",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_cuma": "1",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_cumartesi": "0",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_pazar": "0",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_open": "800",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_close": "1200",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_city": "41",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_subcity": "505",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_adress": "asd",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_tarif": "asda",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_lat": "40.5880381636505",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "loc_lng": "30.481870596093742"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }}jQuery.each(props, function(key, value) {&nbsp; $.each(value.sub_citys, function(key2, value2) {&nbsp; &nbsp; $.each(value2.wtype, function(key3, value3) {&nbsp; &nbsp; &nbsp; $.each(value3.locations, function(key4, value4) {&nbsp; &nbsp; &nbsp; &nbsp; var Location = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; coords: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lat: parseFloat(value4.loc_lat),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lng: parseFloat(value4.loc_lng)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; iconImage: value3.wtype_icon,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; content: '<div style="text-align:center;"><h5>' + value4.loc_name + '</h5><a>' + value4.loc_adress + '</a><br/><small>' + value4.loc_tarif + '</small><hr/>'&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; console.log(Location)&nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; })&nbsp; })})<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>由于您的功能尚未完成,我选择暂时只记录该Location对象。我还不确定要回答的是,“我的数组”包含比“我的对象”(又名props)更多的元素。值应该是null?保持原样?
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript