Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery each always sort it?

I have this object in JS:

var list = {134 : "A",140 : "B",131 : "C"}

I run it with:

jQuery.each(list, function(key, value) { 
console.log(key + " - " + value);
});

The output should be:

134 - A
140 - B
131 - C

But I dont know why, the output is:

131 - C
134 - A
140 - B

Any idea how can I fix it ?

like image 565
Klian Avatar asked Oct 16 '12 12:10

Klian


People also ask

What is each() in jQuery?

jQuery Misc each() Method The each() method specifies a function to run for each matched element. Tip: return false can be used to stop the loop early.

What is each() in javascript?

each(), which is used to iterate, exclusively, over a jQuery object. The $. each() function can be used to iterate over any collection, whether it is an object or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time.

How to break the each loop in jQuery?

To break a $. each or $(selector). each loop, you have to return false in the loop callback. Returning true skips to the next iteration, equivalent to a continue in a normal loop.

How to loop jQuery array?

Answer: Use the jQuery. each() function each() or $. each() can be used to seamlessly iterate over any collection, whether it is an object or an array. However, since the $. each() function internally retrieves and uses the length property of the passed array or object.

How to sort list alphabetically using jQuery?

Given a list of elements, The task is to sort them alphabetically and put each element in the list with the help of jQuery. jQuery text () Method: This method set/return the text content of the selected elements. If this method is used to return content, it provides the text content of all matched elements (HTML tags will be removed).

What is the use of each in jQuery?

What is jQuery.each () jQuery’s each () function is used to loop through each element of the target jQuery object — an object that contains one or more DOM elements, and exposes all jQuery functions. It’s very useful for multi-element DOM manipulation, as well as iterating over arbitrary arrays and object properties.

How to iterate through a list of items in jQuery?

You can select the list items and iterate across them: A message is thus logged for each item in the list: You can stop the loop from within the callback function by returning false. Note: most jQuery methods that return a jQuery object also loop through the set of elements in the jQuery collection — a process known as implicit iteration.

How do you select all the elements in a jQuery page?

Basic jQuery.each () Function Example Let’s see how the jQuery.each () function helps us in conjunction with a jQuery object. The first example selects all the a elements in the page and outputs their href attribute: $('a').each(function(index, value){ console.log(this.href); });


1 Answers

First off: that's not a list, it's an object. Object's order is not guaranteed to be kept - each implementation may choose a different ordering.

On the other hand, arrays do preserve order:

var list = [[134, "A"],[140, "B"],[131, "C"]];


jQuery.each(list, function(i, obj) { 
  console.log(i + " - " + obj[0] + " - " + obj[1]);
});
like image 89
Jakub Hampl Avatar answered Sep 19 '22 15:09

Jakub Hampl