0

I'm looking for a way to reorder an array in javascript moving elements to a specific position.

My array is:

[
    {
        "id": 105142,
        "parent": null,
        "created": "2015-03-20T17:21:33.013+01:00"
    },
    {
        "id": 105150,
        "parent": null,
        "created": "2015-03-20T19:02:09.079+01:00"
    },
    {
        "id": 105160,
        "parent": null,
        "created": "2015-03-20T22:15:06.543+01:00"
    },
    {
        "id": 105190,
        "parent": null,
        "created": "2015-03-21T19:39:19.441+01:00"
    },
    {
        "id": 105193,
        "parent": null,
        "created": "2015-03-22T01:42:00.974+01:00"
    },
    {
        "id": 105195,
        "parent": 105193,
        "created": "2015-03-22T11:05:20.033+01:00"
    },
    {
        "id": 105197,
        "parent": null,
        "created": "2015-03-22T11:06:15.241+01:00"
    },
    {
        "id": 105203,
        "parent": 105193,
        "created": "2015-03-22T22:22:48.833+01:00"
    }
]

I have to move items where parent !== null under their parent. The result so will be:

[
    {
        "id": 105142,
        "parent": null,
        "created": "2015-03-20T17:21:33.013+01:00"
    },
    {
        "id": 105160,
        "parent": 105142,
        "created": "2015-03-20T22:15:06.543+01:00"
    },
    {
        "id": 105150,
        "parent": null,
        "created": "2015-03-20T19:02:09.079+01:00"
    },
    {
        "id": 105190,
        "parent": null,
        "created": "2015-03-21T19:39:19.441+01:00"
    },
    {
        "id": 105193,
        "parent": null,
        "created": "2015-03-22T01:42:00.974+01:00"
    },
    {
        "id": 105195,
        "parent": 105193,
        "created": "2015-03-22T11:05:20.033+01:00"
    },
        {
        "id": 105203,
        "parent": 105193,
        "created": "2015-03-22T22:22:48.833+01:00"
    },
    {
        "id": 105197,
        "parent": null,
        "created": "2015-03-22T11:06:15.241+01:00"
    }
]

Is there any js library to move efficiently the items without a lot of recursions?

1
  • Why do you fear recursion? Commented Mar 27, 2015 at 9:16

3 Answers 3

1

Use Array.prototype.sort should be a good idea. The only tricky thing is the key for comparing. Here, A child node should be greater than it's parent; A child and its parent should both greater or lesser than other nodes. So compare the keys by the keys of itself and its parents. The following code shows how to do that:

var re = {};
for (var i = 0, l = a.length; i < l; i++) re[a[i].id] = a[i];
var id = function (x) {
  var r = [x];
  for (; x; x = re[x].parent) {
    r.unshift(x);
  }
  return r.join('-');
};
a.sort(function (x, y) {
  if (x === y) return 0;
  if (id(x.id) > id(y.id)) return 1; else return -1;
});

Here, a is the array.

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

Comments

0

I think you don't need a library:

Ckeck arrays operations here and check splice()

The powerful splice() method lets you add and/or remove one or more elements from any point in an array:

You just have to find object, store it, remove from the array and insert in the new position.

Comments

0

No need for recursion, two nested loops will do:

data = [
    {
        "id": 105142,
        "parent": null,
        "created": "2015-03-20T17:21:33.013+01:00"
    },
    {
        "id": 105150,
        "parent": null,
        "created": "2015-03-20T19:02:09.079+01:00"
    },
    {
        "id": 105160,
        "parent": null,
        "created": "2015-03-20T22:15:06.543+01:00"
    },
    {
        "id": 105190,
        "parent": null,
        "created": "2015-03-21T19:39:19.441+01:00"
    },
    {
        "id": 105193,
        "parent": null,
        "created": "2015-03-22T01:42:00.974+01:00"
    },
    {
        "id": 105195,
        "parent": 105193,
        "created": "2015-03-22T11:05:20.033+01:00"
    },
    {
        "id": 105197,
        "parent": null,
        "created": "2015-03-22T11:06:15.241+01:00"
    },
    {
        "id": 105203,
        "parent": 105193,
        "created": "2015-03-22T22:22:48.833+01:00"
    }
]

result = [];

data.forEach(function(x) { 
    if(!x.parent) {
        result.push(x);
        data.forEach(function(y) {
            if(y.parent == x.id)
                result.push(y)
        })
    }
})

document.write("<pre>" + JSON.stringify(result,0,3));

This is "quadratic" in time, but will work fine unless you have millions of elements.

1 Comment

I will have max 100 items in my array.

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.