I create 2 functions for the prototype object, and errors are added to the console:

jquery-2.2.3.min.js: 2 Uncaught TypeError: W [g] .exec is not a function

Perhaps something is in conflict with jquery , tell me how to solve the problem?

Here is an example of the functions:

 Object.prototype.getFirstKey = function () { for (var i in this) { return i; break; } } Object.prototype.getFirstValue = function () { for (var i in this) { return this[i]; break; } } 

    1 answer 1

    Modifying embedded objects is a very bad practice; do not use it.
    This is not jQuery 2.2.3 :

     Object.prototype.getFirstKey = function() { for (var i in this) { return i; break; } } Object.prototype.getFirstValue = function() { for (var i in this) { return this[i]; break; } } let test = { hello: 'world', some: 'value' }; console.info(test.getFirstKey(), ' | ', test.getFirstValue()); console.info('Всё работает без ошибок, версия jQuery: %s', $().jquery); 
     <script src="https://code.jquery.com/jquery-2.2.3.min.js" integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo=" crossorigin="anonymous"></script> 

    UPDATE:

     let arr = JSON.parse`[ { "hash1": { "color": "blue", "size": "1", "count": 1 } }, { "hash2": { "color": "red", "size": "22", "count": 22 }},{ "hash3": { "color": "red", "size": "22", "count": 22 }}]`; // Перебираем объекты объектов arr.forEach(e => { // Перебираем сами объекты Object.keys(e).forEach(hash => { console.info(hash, e[hash]); }); }); 

    • Well, in my case I don’t know of another option how not to change the embedded object is simple. I would like to clarify the phrase "This is not jQuery 2.2.3:" do you mean that I do not have 2.2.3 and therefore an error? - user3319778
    • I mean that there is no error on version 2.2.3. You are unlikely to change the error message (and if so, this is stupid, we can not help), so your problem is not that. Tell me the reasons for changing global objects, I just know only one practical reason and you have not done it. - user207618 pm
    • the reason for the necessary parsing is a bit of a non-simple data structure, perhaps there is an easier way, but this =) came to my mind: var myBasketKey = [{"hash1": {"color": "blue", "size": "1" , "count": 1}}, {"hash2": {"color": "red", "size": "22", "count": 22}}, {...}] - and put it all in local storadzh - user3319778
    • one
      @ user3319778, this is solved by any cycle and certainly not by changing the Object prototype. - user207618 4:41 pm
    • @ user3319778, updated example, brute force works fine. True, the structure is slightly poorly constructed - why wrap the array if the object copes well and the depth is less? - user207618