admin管理员组

文章数量:1401438

I have json object like this

[
  { "id":"1", "item1":"item1 text","item2":"item2text","item3":"item3text" },
  { "id":"2", "item1":"item1 text","item2":"item2text","item3":"item3text" }
]

My question is I want to move id 2 to first position through a function by passing the id of the element. like

function movetotop(id){
  //Code here to move id 2 to the top position 
}

Hope someone help.

regards

I have json object like this

[
  { "id":"1", "item1":"item1 text","item2":"item2text","item3":"item3text" },
  { "id":"2", "item1":"item1 text","item2":"item2text","item3":"item3text" }
]

My question is I want to move id 2 to first position through a function by passing the id of the element. like

function movetotop(id){
  //Code here to move id 2 to the top position 
}

Hope someone help.

regards

Share Improve this question edited Apr 30, 2012 at 8:23 Alex Wayne 187k52 gold badges328 silver badges360 bronze badges asked Apr 30, 2012 at 8:16 Murtaza Khursheed HussainMurtaza Khursheed Hussain 15.3k7 gold badges61 silver badges83 bronze badges 3
  • 2 You mean you have a Javascript array with two Javascript objects inside? There's no JSON here. – Jon Commented Apr 30, 2012 at 8:18
  • yeah I only pasted the object data, not the whole object. – Murtaza Khursheed Hussain Commented Apr 30, 2012 at 8:24
  • This edit might be wrong, then. Is this an array of objects? [ {}, {} ] or an object containing other objects? { "item1" : {}, "item2": {} } – Visionary Software Solutions Commented Apr 30, 2012 at 8:28
Add a ment  | 

4 Answers 4

Reset to default 4
function moveIdToTop(jsonarray, id) {
 for (var i = 0; i < jsonarray.length; ++i) {
    if (jsonarray[i].id == id) {
       var temp = jsonarray[i];
       jsonarray.splice(i, 1);
       jsonarray.unshift(temp);
       break;
    }
 }
}

If you have an array of objects, use the sort method:

var data = [
 { "id":"1", "item1":"item1 text","item2":"item2text","item3":"item3text" },
 { "id":"2", "item1":"item1 text","item2":"item2text","item3":"item3text" }
];

var sorted = data.sort(function(a, b) {
    return a.id < b.id;
});

This will move 2 to the top. Tweak the sort function to match your needs.

Assuming that they are in an array, here's a demo:

function popTop(arr, id) {
    var i, al = arr.length;
    dance: for (i = 0; i < al; i++) {
        if (arr[i].id == id) {
            arr.unshift(arr.splice(i, 1)[0]);
            break dance;
        }
    }
}

You need to extract the element from the array and re-insert it as the first one.

var yourarray = [{ "id":"1", "item1":"item1 text","item2":"item2text","item3":"item3text" }, 
                 { "id":"2", "item1":"item1 text","item2":"item2text","item3":"item3text" }];

function movetotop(id){
    //Code here to move id 2 to the top position
    var index = yourarray.map(function(element){return element.id;}).indexOf(id);
    if (index > -1) {
        var extracted = yourarray.splice( index,1 )[0];
        yourarray.unshift(extracted);
    }
}

Use with movetotop('2');

Demo at http://jsfiddle/gaby/NGjXy/

本文标签: javascriptMove item in json arrayStack Overflow