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
4 Answers
Reset to default 4function 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
版权声明:本文标题:javascript - Move item in json array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744231761a2596370.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论