JSON-ni tanlangan ochiladigan opsiyaga asoslangan holda filtrlash

Ikki tugmachani o'z ichiga olgan mamlakatlar ro'yxatiga ega bo'lgan JSON fayli bor: "name" va "isacode". JSON faylida "value" qiymati bo'lgan JSON faylidagi "nom" kaliti bilan bir xil variantlar ro'yxatini ochaman.

Uzoq qisqacha hikoya: JSON faylidagi "isacode" qiymatini JSON faylidagi "nom" qiymatiga mos keladigan tanlangan parametr bilan ko'rsatishni istayman

Screenshot

UPDATE

I have pasted the code here: http://codepen.io/sarfehjou/pen/RpKMWE

Yagona farq shundaki, mening yechimda JSON faylini url orqali olish kerak

HTML

   

Skript

    var list = {
    "countries": [
        {
            "name": "Afghanistan GLOBAL",
            "areacode": ""
        },
        {
            "name": "Albania",
            "areacode": ""
        },
        {
            "name": "Algeria",
            "areacode": ""
        },
        {
            "name": "American Samoa",
            "areacode": ""
        },
        {
            "name": "Andorra",
            "areacode": ""
        }
      ]
    }
1
savolga matn shaklida tegishli barcha ma'lumotlarni kiriting. bu erda ham ko'rishingiz mumkin Minimal, to'liq va tasdiqlanadigan misol
qo'shib qo'ydi muallif Nina Scholz, manba
savolga matn shaklida tegishli barcha ma'lumotlarni kiriting. bu erda ham ko'rishingiz mumkin Minimal, to'liq va tasdiqlanadigan misol
qo'shib qo'ydi muallif Nina Scholz, manba
savolga matn shaklida tegishli barcha ma'lumotlarni kiriting. bu erda ham ko'rishingiz mumkin Minimal, to'liq va tasdiqlanadigan misol
qo'shib qo'ydi muallif Nina Scholz, manba
Sizni to'xtatish nimani anglatadi?
qo'shib qo'ydi muallif 31piy, manba

6 javoblar

Sizning savolingiz allaqachon tashkil etilganidek, noaniq, ya'ni hudud kodi va narsalari bilan nima qilish kerakligiga ishonchingiz komil emasmi? Men baribir bunga erishishim mumkin:

asosan tanlovda bir voqea tinglovchisini qo'shdi va siz uni o'zgartirganda, ro'yxatdagi qiymatni (ro'yxat serverdan yoki keshlangan bo'lishi mumkin) ko'rib chiqadi.

Ko'rinib ko'ring, agar siz ko'proq g'oyalarni olsangiz, yana bir narsa so'raydi.

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

    var list = {
    "countries": [
        {
            "name": "Afghanistan GLOBAL",
            "areacode": "8787"
        },
        {
            "name": "Albania",
            "areacode": "446"
        },
        {
            "name": "Algeria",
            "areacode": "212"
        },
        {
            "name": "American Samoa",
            "areacode": "767"
        },
        {
            "name": "Andorra",
            "areacode": "5454"
        }
      ]
    }
    
    var countrySelect = document.getElementById('countrySelect');
    
    countrySelect.addEventListener('change', function(e)  {
      console.log(this.value);
      console.log(findAreaCode(this.value));
    });
    
    function findAreaCode(name) {
      var resultItems = list.countries.filter(function(currentItem) {
        return currentItem.name == name;
      })
      console.log(resultItems[0]);
      
      return resultItems[0]? resultItems[0].areacode: 'N/A';
    }
    
    
  
</div> </div>
1
qo'shib qo'ydi
mamnuniyat bilan gaplasha oladigan ... stackoverflow xush kelibsiz, javobni keyin qabul qiling.
qo'shib qo'ydi muallif Mosd, manba
Ajoyib! Jozibasi kabi ishlar :) rahmat
qo'shib qo'ydi muallif Arash, manba

Sizning savolingizga javob berish uchun buni qilishingiz mumkin,

$.getJSON("http://api/somejsonfile/",function(data){
   //Initializing an empty array
    var options=[];
   //Running a loop on the response JSON and adding the options to a string array
    $.each(data.countries,function(id,eachcountry){
        options.push("
");
    });
   //Joining the string and replacing it inside the select element 
    $(".dropdown").html(options.join(""));
});

Bu juda qo'pol dasturdir, shuning uchun ba'zi sintaktik masalalar bo'lishi mumkin!

Oddiy misolni qo'shish

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

$(document).ready(function(){
// This is a sample country code JSON from Github
$.getJSON("https://gist.githubusercontent.com/Goles/3196253/raw/9ca4e7e62ea5ad935bb3580dc0a07d9df033b451/CountryCodes.json",function(data){
        // Initializing an empty array
        var options=[];
        // Running a loop on the response JSON and adding the options to a string array
        $.each(data,function(id,eachcountry){
                options.push("
");
        });
        // Joining the string and replacing it inside the select element 
        $(".dropdown").html(options.join(""));
    });
    
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</div> </div>
0
qo'shib qo'ydi

Sizning savolingizga javob berish uchun buni qilishingiz mumkin,

$.getJSON("http://api/somejsonfile/",function(data){
   //Initializing an empty array
    var options=[];
   //Running a loop on the response JSON and adding the options to a string array
    $.each(data.countries,function(id,eachcountry){
        options.push("
");
    });
   //Joining the string and replacing it inside the select element 
    $(".dropdown").html(options.join(""));
});

Bu juda qo'pol dasturdir, shuning uchun ba'zi sintaktik masalalar bo'lishi mumkin!

Oddiy misolni qo'shish

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

$(document).ready(function(){
// This is a sample country code JSON from Github
$.getJSON("https://gist.githubusercontent.com/Goles/3196253/raw/9ca4e7e62ea5ad935bb3580dc0a07d9df033b451/CountryCodes.json",function(data){
        // Initializing an empty array
        var options=[];
        // Running a loop on the response JSON and adding the options to a string array
        $.each(data,function(id,eachcountry){
                options.push("
");
        });
        // Joining the string and replacing it inside the select element 
        $(".dropdown").html(options.join(""));
    });
    
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</div> </div>
0
qo'shib qo'ydi

I have pasted the code her: http://codepen.io/sarfehjou/pen/RpKMWE

Yagona farq, mening yechimda json faylini url bilan olish kerak

   

    var list = {
    "countries": [
        {
            "name": "Afghanistan GLOBAL",
            "areacode": ""
        },
        {
            "name": "Albania",
            "areacode": ""
        },
        {
            "name": "Algeria",
            "areacode": ""
        },
        {
            "name": "American Samoa",
            "areacode": ""
        },
        {
            "name": "Andorra",
            "areacode": ""
        }
      ]
    }
0
qo'shib qo'ydi
Buni o'chirib tashlang, men uni savolga qo'shib qo'ydim. Aks holda salbiy reyting olishingiz mumkin.
qo'shib qo'ydi muallif Arcanyx, manba
Yuqorida siz kiritgan kod savolning bir qismidir, to'g'rimi? Savolingizda siz yuqorida ko'rsatilgan narsaga qo'shib qo'ydim, shunda siz ushbu javobni o'chirib tashlashingiz mumkin ...
qo'shib qo'ydi muallif Arcanyx, manba
Men tushunmayapman, savolga nima qo'shding? va nima yo'q qilishim kerak?
qo'shib qo'ydi muallif Arash, manba

I have pasted the code her: http://codepen.io/sarfehjou/pen/RpKMWE

Yagona farq, mening yechimda json faylini url bilan olish kerak

   

    var list = {
    "countries": [
        {
            "name": "Afghanistan GLOBAL",
            "areacode": ""
        },
        {
            "name": "Albania",
            "areacode": ""
        },
        {
            "name": "Algeria",
            "areacode": ""
        },
        {
            "name": "American Samoa",
            "areacode": ""
        },
        {
            "name": "Andorra",
            "areacode": ""
        }
      ]
    }
0
qo'shib qo'ydi
Buni o'chirib tashlang, men uni savolga qo'shib qo'ydim. Aks holda salbiy reyting olishingiz mumkin.
qo'shib qo'ydi muallif Arcanyx, manba
Yuqorida siz kiritgan kod savolning bir qismidir, to'g'rimi? Savolingizda siz yuqorida ko'rsatilgan narsaga qo'shib qo'ydim, shunda siz ushbu javobni o'chirib tashlashingiz mumkin ...
qo'shib qo'ydi muallif Arcanyx, manba
Men tushunmayapman, savolga nima qo'shding? va nima yo'q qilishim kerak?
qo'shib qo'ydi muallif Arash, manba

I have pasted the code her: http://codepen.io/sarfehjou/pen/RpKMWE

Yagona farq, mening yechimda json faylini url bilan olish kerak

   

    var list = {
    "countries": [
        {
            "name": "Afghanistan GLOBAL",
            "areacode": ""
        },
        {
            "name": "Albania",
            "areacode": ""
        },
        {
            "name": "Algeria",
            "areacode": ""
        },
        {
            "name": "American Samoa",
            "areacode": ""
        },
        {
            "name": "Andorra",
            "areacode": ""
        }
      ]
    }
0
qo'shib qo'ydi
Buni o'chirib tashlang, men uni savolga qo'shib qo'ydim. Aks holda salbiy reyting olishingiz mumkin.
qo'shib qo'ydi muallif Arcanyx, manba
Yuqorida siz kiritgan kod savolning bir qismidir, to'g'rimi? Savolingizda siz yuqorida ko'rsatilgan narsaga qo'shib qo'ydim, shunda siz ushbu javobni o'chirib tashlashingiz mumkin ...
qo'shib qo'ydi muallif Arcanyx, manba
Men tushunmayapman, savolga nima qo'shding? va nima yo'q qilishim kerak?
qo'shib qo'ydi muallif Arash, manba
Javascript UZB
Javascript UZB
99 ishtirokchilar

@js_uzb @vuejs_uz @react_uz @nodejs_uz @angular_uz @ngTashkent @yiiframework_uz @laravel_uz @linux_uzbek @python_uz @swift_uzb —————— @uzdevgroup @UzGeeksGroup ——— @UzDev_Jobs @jobs_uzb