Good day to all! I'm trying to solve this problem for the 4th day (apparently my knowledge of js is still not enough)

So what we have is such a code ...

$.ajax({ type: "GET", async: false, url: "cb.xml", dataType: "xml", success: currencyRate }); function currencyRate(xml) { $(xml).find("Valute").each(function() { var currencyID = $(this).attr('ID'); //ID валюты var currencyValue = $(this).find("Value").text().replace(',', '.'); //курс валюты console.log(currencyID); console.log(currencyValue); }); }; 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 

The code parses the xml file, (link if anything to XML ) pulls out the current exchange rate from it. What I need to do, I need to create an object, by type ...

 var currency = { R01775: /*ID валюты*/ '70.3548', /*курс валюты*/ R01760: /*ID валюты*/ '15.0177', /*курс валюты*/ // итд... }; 

or such a CharCode object: 'Value'

And then we should work with this object ...

    2 answers 2

    Creating an object in code is easy.

     var object = {}; 

    Further, the fields are dynamically added to the object as follows:

     object["fieldName"] = value; 

    since "fieldName" is any line, this assignment is easy to do in a loop.

    For example:

     for(var i=0;i<5;i++) { object["x"+i] = "value:"+i; } 
    • Thank! I'll try it too. - Mistio
    • @Mistio This is the same way that teran wrote, only he wrote for a specific case, and I explained the syntax itself, the theory. - Dmitry Polyanin
    • @Mistio If the answer was useful, you can put UpVote. - Dmitry Polyanin
    • Theory is important too! but now I try to do more practice - Mistio
    • @Mistio Well, in this case, this is the theory that is necessary for practice)) not an abstract but a specific method, what is needed for understanding how the dynamic setting of properties works. - Dmitry Polyanin

    What is the difficulty then? enter object fields and values ​​and work

     var data = '<ValCurs Date="08.12.2017" name="Foreign Currency Market"><Valute ID="R01010"><NumCode>036</NumCode><CharCode>AUD</CharCode><Nominal>1</Nominal><Name>Австралийский доллар</Name><Value>44,6015</Value></Valute><Valute ID="R01020A"><NumCode>944</NumCode><CharCode>AZN</CharCode><Nominal>1</Nominal><Name>Азербайджанский манат</Name><Value>34,8793</Value></Valute></ValCurs>'; function currencyRate(xml) { var currency = { }; $(xml).find("Valute").each(function() { var currencyID = $(this).attr('ID'); //ID валюты var currencyValue = $(this).find("Value").text().replace(',', '.'); //курс валюты currency[currencyID] = parseFloat(currencyValue); }); console.log(currency); return currency; }; currencyRate( $.parseXML(data) ); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

    • If I understand correctly, do you suggest saving all file fields to a variable? XML - file is dynamic, it can be replaced constantly, it all depends on the exchange rate. There is a piece of PHP code that downloads this file from the Central Bank to my server. - Mistio
    • @Mistio Not all, but necessary. But didn't you want this? - Igor
    • @Mistio did not understand the question. If you are talking about the data variable, then this is data instead of your ajax request. If about the currency then the question you did not specify which fields are needed or not, all currencies from xml - teran
    • Maybe I did not explain, litter if that. Once again, I want to put all currencies from an XML file into an object, and then work with this object. Rather, I need current courses, and access to courses in the object will be either by id or by CharCode - Mistio
    • one
      @Mistio Does the above code do the same? data as I said here for the demonstration. - teran