在javascript中移动对象元素位置 [英] Move object element position in javascript
本文介绍了在javascript中移动对象元素位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试找到一种将对象元素移动到特定位置的方法.
I'm trying to find a way to move an object element to a specific position.
例如,我有这个对象:
{
"element1" : {} // object,
"element2" : {},
"element3" : {}
}
我搜索将 element3 移动到第一个位置:
And I search to move element3 at the first position :
{
"element3" : {},
"element1" : {},
"element2" : {}
}
感谢您的帮助!
推荐答案
JavaScript 对象没有顺序.您应该改用一组对象.
JavaScript objects don't have ordering. You should use an array of objects instead.
var arr = [
{ id: 'element1' },
{ id: 'element2' },
{ id: 'element3' }
]
然后你可以像这样重新排序:
You could then reorder it something like:
var first = arr.splice(0, 1);
arr.push(first);
也许你可以根据 id 获取特定元素:
Maybe you could then grab specific elements based on id:
var out = arr.filter(function (el) {
return el.id === 'element1';
});
这篇关于在javascript中移动对象元素位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文