0

I need to save order in array. A normal array was destroying it, so i found associative array, but with indexes from regexp was sorting records too. My function is

var myArray = {};
var searchIndex = '';
$("#searchList").find('li').each(function( index ) {
    id = $( this ).attr('id');
    if(id.match(/search(:?\d+|\w+)/)){
        searchIndex = id.match(/search(\d+|\w+)/)[1]; 
        myArray[searchIndex] = "empty";
    }
});

This code works well, order are saved.

myArray[id] = "empty"; 

http://screenshooter.net/100008827/fhbsvjm But when i want to remove string "search" from id, by regexp, array just sorting indexes...

searchIndex = id.match(/search(\d+|\w+)/)[1]; 
myArray[searchIndex] = "empty";

http://screenshooter.net/100008827/gmxusyu But order should be last->7->9->8

2
  • 1
    There is no guarantee on the order of keys in a JavaScript object. If you want to preserve the order of a list of items, your best bet may be to build your own data structure that stores an ordinary array internally and provides a key-based lookup to the items in it. Commented Jan 5, 2015 at 15:35
  • My array are changing every time, so i need to build structure every time. It looks like that, but in array(3,1,10) can be more than 3 rows too. screenshooter.net/100008827/dcpnjmp Commented Jan 5, 2015 at 15:51

1 Answer 1

1

JavaScript does not have associative arrays. The plain objects in JavaScript are similar to associative arrays in some ways, but the order of their properties is not guaranteed.

If you want an associative array that preserves the order of items in the order they were added, it is possible to create one from scratch. Here is a fairly simple implementation that provides that functionality:

function AssociativeArray() {
  var items = [];
  var refs = {};

  this.set = function(key, value) {
    if (key in refs) {
      refs[key].value = value;
    } else {
      var entry = { key: key, value: value };
      items.push(entry);
      refs[key] = entry;
    }
  };

  this.get = function(key) {
    var entry = refs[key];
    return entry && entry.value;
  };

  this.allItems = function() {
    return items.slice();
  };
}

var assoc = new AssociativeArray();

assoc.set(7, "hello");
assoc.set(3, "goodbye");
assoc.set("cheer", "yay");

var items = assoc.allItems();
for (var i = 0; i < items.length; i += 1) {
   console.log(items[i].key + " - " + items[i].value);
}
console.log("The item with key 7 is: " + assoc.get(7));

The way you would adapt this to your current code is:

var myArray = new AssociativeArray();
$("#searchList").find('li').each(function( index ) {
    var id = $( this ).attr('id'),
        searchIndex;
    if(id.match(/search(:?\d+|\w+)/)){
        searchIndex = id.match(/search(\d+|\w+)/)[1]; 
        myArray.set(searchIndex, "empty");
    }
});

The first code snippet above shows how to iterate through the array in the order that items were added.

Sign up to request clarification or add additional context in comments.

4 Comments

Im still trying, but i cant do it... I just append to normal array all my records and I edited it in PHP. From this array screenshooter.net/100008827/rliqutk to screenshooter.net/100008827/ulktobh . By this code pastebin.com/dPnFD5XX . Can u show me how it should look in javascript?
Nope screenshooter.net/100008827/obdmpbe . I just forgot to say, I need this array to pass through ajax. Btw. look at my comment, in first array u can see order of values and how array should looks.
@MichałSzałapski Which first array? The one that has json[]: searchlast, json[]: 22, json[]: 27,...? Please edit your question and show a sample of an input (this would presumably be in <li> elements and expected output (this would presumably be in JSON). Your requirements are not clear from these screenshots you keep providing. Where is the code that you are using to send these values over AJAX?
pastebin.com/L2C8yNmW from this javascript i getting this array screenshooter.net/100008827/rliqutk , for second array, check my first comment, which i sorting in PHP. Id from <li> loop, are search12 -> group321 -> group10 ->group5->searchlast->group7 etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.