Sorting an object by array inside an array?

I have a local storage that looks like this:

Key: Savedme        
Value:
{
 "Bob":["1","1"],
 "John":["2","1"],
 "Mom":["3","1"],
 "Dad":["1","2"],
 "Tom":["3","2"],
 "Skipper42":["2","3"],
 "Hated_41":["3","3"],
 "Greeneggs":["2","2"],
 "William":["1","3"]
}

I need to sort it somehow to look like this

{
 "Bob":["1","1"],
 "Dad":["1","2"],
 "William":["1","3"]
 "John":["2","1"],
 "Greeneggs":["2","2"],
 "Skipper42":["2","3"],
 "Mom":["3","1"],
 "Tom":["3","2"],
 "Hated_41":["3","3"]
}

I tried to save it in a matrix such as:

var $runthrough = [[]];
$runthrough[$x,$y] = $values;

Where x is the first set of numbers, y is the next, and then the values ​​are Bob, Dad, etc .... from there I could just do foreach for both sections of the matrix, and this will be done, HOWEVER, when I use this method after it goes through one set of objects, the second set gives "undefined", although I set some triggers to check, and in fact this does not happen undefined.

var loadarray = JSON.parse(localStorage.getItem( 'savedme' ));
$.each(loadarray, function(k, v) {
     if(typeof k === 'undefined' || !k){
        console.error("undefined found at k!");
    };
     if(typeof v[0] === 'undefined' || !v[0]){
        console.error("undefined found at x!");
    };
     if(typeof v[1] === 'undefined' || !v[1]){
         console.error("undefined found at y!");
    };
});

, , , - , , , THEN . , , :

  {
    "1":["1","Bob"],
    "2":["1","John"],
  } 

... 1

+4
3

JavaScript, Array.

ECMAScript Third Edition (pdf):

4.3.3
. , , . , .

​​ , :

[
 { name: "Bob", value: ["1","1"] },
 { name: "Dad", value: ["1","2"] },
 { name: "William", value: ["1","3"] },
 { name: "John", value: ["2","1"] },
 { name: "Greeneggs", value: ["2","2"] },
 { name: "Skipper42", value: ["2","3"] },
 { name: "Mom", value: ["3","1"] },
 { name: "Tom", value: ["3","2"] },
 { name: "Hated_41", value: ["3","3"] }
]

:

var loadarray = JSON.parse(localStorage.getItem( 'savedme' ));
var sorted = [];
for (var prop in loadarray) {
    if (loadarray.hasOwnProperty(prop)) {
        sorted.push({name:prop, value:loadarray[prop]});
    }
}
sorted.sort(function(a, b) {
    var v0 = a.value[0] - b.value[0];
    return v0 == 0 ? a.value[0] - a.value[0] : v0;
});
+2

var a = [
  { "Bob": ["1", "1"] },
  { "John": ["2", "1"] },
  { "Mom": ["3", "1"] },
  { "Dad": ["1", "2"] },
  { "Tom": ["3", "2"] },
  { "Skipper42": ["2", "3"] },
  { "Hated_41": ["3", "3"] },
  { "Greeneggs": ["2", "2"] },
  { "William": ["1", "3"] }
];

a.sort(function (a, b) {
  var aa = a[Object.keys(a)],
      bb = b[Object.keys(b)];
  if (aa[0] === bb[0]) {
    return aa[1] - bb[1];
  } else {
    return aa[0] - bb[0];
  }
});
document.querySelector("#demo").innerHTML = JSON.stringify(a, null, 4);
<div id="demo"></div>
Hide result
+2

Keys cannot be sorted inside an object.

However, they can be processed in order using Object.keys(object).sort().

Here I output the object to an array - sorted by key values ​​- then display this array:

var obj= {
 "Bob":["1","1"],
 "John":["2","1"],
 "Mom":["3","1"],
 "Dad":["1","2"],
 "Tom":["3","2"],
 "Skipper42":["2","3"],
 "Hated_41":["3","3"],
 "Greeneggs":["2","2"],
 "William":["1","3"]
}

var arr= Object.keys(obj)
          .sort(function(a, b) {
            if(obj[a][0]===obj[b][0]) {
              return obj[a][1] - obj[b][1];
            }
            else {
              return obj[a][0] - obj[b][0];
            }
          })
          .map(function(key) {
            var o= {};
            o[key]= obj[key];
            return o;
          });

document.body.innerHTML= JSON.stringify(arr);
Run codeHide result
0
source

All Articles