0

I have an object type as below:

{
"1": {
    "ref": "1",
    "active": "1",
    "showorder": "1",
    "title": "Test 1"
},
"2": {
    "ref": "2",
    "active": "1",
    "showorder": "2",
    "title": "Test 2"
},
"3": {
    "ref": "3",
    "active": "1",
    "showorder": "4",
    "title": "Test 4"
},
"4": {
    "ref": "4",
    "active": "1",
    "showorder": "9",
    "title": "Test 9"
},
"5": {
    "ref": "5",
    "active": "1",
    "showorder": "7",
    "title": "Test 7"
}
}

On the basis of showorder property i need it to be arranged as follows:

{
"1": {
    "ref": "1",
    "active": "1",
    "showorder": "1",
    "title": "Test 1"
},
"2": {
    "ref": "2",
    "active": "1",
    "showorder": "2",
    "title": "Test 2"
},
"3": {
    "ref": "3",
    "active": "1",
    "showorder": "3",
    "title": "Test 3"
},
"4": {
    "ref": "4",
    "active": "1",
    "showorder": "4",
    "title": "Test 4"
},
"5": {
    "ref": "5",
    "active": "1",
    "showorder": "5",
    "title": "Test 5"
}
}

And here is my failed attempt thought i would pass the object to this function :

function reArrangeTilesObject(tilesObj){
var newObj = [];
for(var i in tilesObj){
    var j = 1;
    if(j == tilesObj[i].showorder){
        newObj.push(tilesObj[i]);
        j++;
    }           
}
return newObj;
}

Sorry if i am being unclear. The object is created in a random manner. So what i want is the object to be arranged as per the showorder property. so if showorder is 4 it should come in the key of 4 and not have the object of showorder 9. showorder 9 should come under the key of 9. Thanks

ABL
  • 3
  • 2
  • What kind of problems did you experience? – phuzi Sep 17 '14 at 13:26
  • This just adds one record to the new object. There isn' t any problem but i cant get to construct the object as per the way i want. – ABL Sep 17 '14 at 13:28
  • Sorry if i am being unclear. The object is created in a random manner. So what i want is the object to be arranged as per the showorder property. so if showorder is 4 it should come in the key of 4 and not have the object of showorder 9. showorder 9 should come under the key of 9. Thanks – ABL Sep 17 '14 at 13:44

5 Answers5

0

I may have misunderstood something but :

for (var i = 0; i < tilesObj; i++)
{
    tilesObj[i].showOrder = i;
    tilesObj[i].title = "Test "+i;
}

or

for (var i in tilesObj)
{
    tilesObj[i].showOrder = tilesObj[i].ref;
    tilesObj[i].title = "Test "+tilesObj[i].ref;
}

will do the job on the example I believe.

Hotted24
  • 502
  • 3
  • 8
0

Steps:

  1. Convert the object to an array (objects are unordered, arrays are ordered)
  2. Sort the array

Code:

function reArrangeTilesObject(tilesObj) {
    var tilesArray = [];

    // Step 1 - convert to array

    for (var i in tilesObj) {
        tilesArray.push(tilesObj[i]);
    }

    // Step 2 - sort

    tilesArray.sort(function(a, b) {
        return a.showorder - b.showorder;
    });

    return tilesArray;
}

Result – an ordered array:

[
    {"ref":"1","active":"1","showorder":"1","title":"Test 1"},
    {"ref":"2","active":"1","showorder":"2","title":"Test 2"},
    {"ref":"3","active":"1","showorder":"4","title":"Test 4"},
    {"ref":"5","active":"1","showorder":"7","title":"Test 7"},
    {"ref":"4","active":"1","showorder":"9","title":"Test 9"}
]
Shai
  • 7,159
  • 3
  • 19
  • 22
  • Missed a bracket there `newArray.push(tilesObj[i]);` – chridam Sep 17 '14 at 13:36
  • Thanks. Fixed a couple of typos after testing – Shai Sep 17 '14 at 13:59
  • Thank you Shai..this works for me.. what is the line of return in the sort array do? didnt get it .. – ABL Sep 17 '14 at 16:23
  • There's some info [on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) but basically: to sort an array, we simply provide a function which takes two of our array elements as parameters `a` and `b` (these could be any of our elements). Our function just needs to return a negative number if we'd like `a` to be sorted before `b`, or positive if `b` should go before `a`. `a.showorder - b.showorder` achieves this. The Javascript sorting algorithm will call our function with 2 elements at a time, until it has learnt the exact order we want. – Shai Sep 17 '14 at 20:19
  • `a.showorder - b.showorder` works because if `a.showorder` is bigger then the result will be positive (which tells the sorting algorithm to put `b` first), and if `b.showorder` is bigger the result of the sum will be negative which means put `a` first. So the objects end up sorted in order of `showorder`. – Shai Sep 17 '14 at 20:25
  • @ABL if that doesn't help let me know which part you're still unsure of. – Shai Sep 17 '14 at 21:40
0

I can't quite understand if you're trying to rename the properties in the object array, or sort them into a new array.

If you're trying to sort:

function reArrangeTilesObject(tilesObj) {       
    var newObj = [];        
    for (var i = 1; i <= tilesObj.length; i++) {
        for (var j in tilesObj) {
            if (i == tilesObj[j].showorder) {
                newObj.push(tilesObj[j]);
                break;
            }
        }
    }
    return newObj;
}

Should add them to your newObj array based on their showorder property.

hotforfeature
  • 2,558
  • 1
  • 16
  • 24
  • Sorry about that.. I need the new object to be arranged based on the showorder property. So if showorder is 3 it should have its key as 3 and should have the object of showorder 3 – ABL Sep 17 '14 at 13:38
  • Mate, Your solution will only sort the array but it wont be in ascending order of the showorder – ABL Sep 17 '14 at 14:30
0

If you are just rewriting the showorder and title values this will work

function fix(o) {
    for(var n in o) {
        o[n].showorder = n;
        o[n].title = "Test " + n;
    }
}
eazimmerman
  • 599
  • 4
  • 20
0

Try this one (demo):

function reArrangeTilesObject(tilesObj){
    var result = {};
    for(var key in tilesObj){
        content = result[key] = tilesObj[key];
        content.showorder = key;
        content.title = "Test " + key;
    }
    return result;
}

It will return a new object in the original format (a Dictionary). If you want to return an Array (and deal with possible empty indexes), just change var result = {}; into var result = [];