0

I have data saved in "localStorage".

Now i'd like to put that data into a JS array so that i can sort it and then output it.

//CREATE ARRAY
var localStorage_arr = [];

//LOOP THREW localStorage
Object.keys(localStorage).forEach(function(key){

  //GET LOCALSTORAGE ITEM
  var item = JSON.parse(localStorage.getItem(key));

  //SET ARRAY KEY AND CREATE MULTIDIMENSIONAL ARRAY
  localStorage_arr[key] = [];

  //ADD SOME DATA
  localStorage_arr[key]['art_id'] = item.art_id;
  localStorage_arr[key]['art_nr'] = item.art_nr;
});

//SORT ARRAY    
localStorage_arr.sort();

//CONSOLE.LOG ARRAY
console.log(localStorage_arr)

//IN LOG:
[1614851259727: Array(0), 1614849876677: Array(0), 1614849865169: Array(0), 1614849873617: Array(0), 1614849870613: Array(0)]
1614849865169: [art_id: 2110, art_nr: "01", article: "Test", balance: 362, …]
1614849870613: [art_id: 2110, art_nr: "01", article: "Test", balance: 362, …]
1614849873617: [art_id: 2110, art_nr: "01", article: "Test", balance: 362, …]
1614849876677: [art_id: 2110, art_nr: "01", article: "Test", balance: 362, …]
1614851259727: [art_id: 2110, art_nr: "01", article: "Test", balance: 380, …]

Question one: Why does it say: Array(0) ?

Later i try to loop threw my array:

//LOOP THREW localStorage_arr
localStorage_arr.forEach(function (item, index) {
    //console.log(item, index);
});

//IN LOG: nothing..

Why can't i loop threw the array?

6
  • Can you share how your localStorage looks like? Commented Mar 4, 2021 at 11:54
  • Is key a number? like an array position? Commented Mar 4, 2021 at 12:15
  • @lissettdm Yes, Key is the date in milliseconds like: 1614851259727 Commented Mar 4, 2021 at 12:55
  • So, you are assuming that key was the element position right? Commented Mar 4, 2021 at 12:56
  • @lissettdm The reason why i'm doing a array of the localStorage is because i need to sort the data by it's key. localStorage doesn't sort or order the keys. So i put the date in millisenconds as a key, for when i add something to the localStorage, then i want to display them in the order it was added. Commented Mar 4, 2021 at 13:01

1 Answer 1

1

it is because of how javascript array work, see JavaScript Array

Arrays cannot use strings as element indexes (as in an associative array) but must use integers. Setting or accessing via non-integers using bracket notation (or dot notation) will not set or retrieve an element from the array list itself, but will set or access a variable associated with that array's object property collection.

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

2 Comments

But the key i set is: 1614851259727 so its a number, right?
@Björn C yes, but it is a BigInt value, but array keys must be of uint32, see explanation here:developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

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.