反向 json javascript

发布于 2024-10-04 11:37:34 字数 802 浏览 0 评论 0原文

是否有一种廉价的方法来反转:

{
    "10": "..."
    "11": "...",
    "12": "...",
    "13": "...",
    "14": "...",
}

以便我得到:

{
    "14": "...",
    "13": "...",
    "12": "..."
    "11": "...",
    "10": "...",
}

reverse() 似乎不适用于 json 对象。我能想到的唯一方法是循环所有元素并创建一个数组。感觉应该有更好的办法。

编辑:感谢您的所有帮助更新:

如果每个键都有按时间顺序排列的数据呢?当我在对象上使用 $.each 时,它从上到下遍历对象,我没有意识到这是不可靠的。

这就是我想要做的:

$.each(object, function (key, value) {
  function foo (key, value);
});

除了最后 3 对之外,我不想对所有其他都运行 foo,也就是说我只想使用最后 3 对。我想如果我能逆转它们,我就可以跑前三个然后停下来。

有什么办法可以只完成最后3个吗?如果最后 3 个排序不可靠,是否有更安全的方法来获取最后 3 个。最后 3 个将具有最大的数字键。

谢谢。

编辑2: 我基本上最终决定在服务器端进行操作。我正在重新组织我的数据库,以便相关子文档现在已包含可以使用 mongodb 查询的文档。谢谢。

Is there an inexpensive way to reverse:

{
    "10": "..."
    "11": "...",
    "12": "...",
    "13": "...",
    "14": "...",
}

so that I get:

{
    "14": "...",
    "13": "...",
    "12": "..."
    "11": "...",
    "10": "...",
}

reverse() doesn't seem to work on json objects. The only way I can think of is to loop through all the elements and create an array. feels like there should be a better way.

Edit: thanks for all the help UPDATE:

What about let's say if each key has chronological data. When I use $.each on the object, it runs through the objects from top to bottom, I didn't realize that was unreliable.

Here's what I'm trying to do:

$.each(object, function (key, value) {
  function foo (key, value);
});

I want to not run foo on all but the last 3 pairs, that is I only want to use the last 3 pairs. I figured if I can reverse them I can just run the first three and stop.

Is there any way I can just do the last 3? If the last 3 ordering is unreliable, is there a safer way to grab the last 3. The last 3 will have the largest numerical keys.

Thanks.

Edit 2:
I'm basically deciding finally to do the manipulations on the server side. I'm reorganizing my database so that the relevant subdocuments are now full on documents that could be queried with mongodb. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

缪败 2024-10-11 11:37:34

JavaScript 关联数组是无序的。您不能依赖于任何特定顺序的属性。

来自 Mozilla 开发者网络

虽然 ECMAScript 进行了迭代
对象的顺序
依赖于实现,它可能
看来所有主流浏览器都支持
基于的迭代顺序
最早添加的属性优先
(至少对于不在
原型)。然而,在以下情况下
Internet Explorer,当人们使用
删除属性,有些令人困惑
行为结果,阻止其他
浏览器禁止使用简单对象
就像有序的对象文字一样
关联数组。在资源管理器中,同时
属性值确实设置为
未定义,如果稍后加回一个
同名财产,
属性将在其旧版本中迭代
位置——不在末尾
正如人们所期望的迭代顺序
删除该属性后并且
然后将其添加回来。

所以如果你想模拟一个有序的
跨浏览器中的关联数组
环境,你被迫要么
使用两个单独的数组(一个用于
键和值的另一个),或
构建一个单一属性的数组
物体等

Javascript associative arrays are unordered. You cannot depend on the properties being in any particular order.

From Mozilla Developer Network:

Although ECMAScript makes iteration
order of objects
implementation-dependent, it may
appear that all major browsers support
an iteration order based on the
earliest added property coming first
(at least for properties not on the
prototype). However, in the case of
Internet Explorer, when one uses
delete on a property, some confusing
behavior results, preventing other
browsers from using simple objects
like object literals as ordered
associative arrays. In Explorer, while
the property value is indeed set to
undefined, if one later adds back a
property with the same name, the
property will be iterated in its old
position--not at the end of the
iteration sequence as one might expect
after having deleted the property and
then added it back.

So if you want to simulate an ordered
associative array in a cross-browser
environment, you are forced to either
use two separate arrays (one for the
keys and the other for the values), or
build an array of single-property
objects, etc.

一影成城 2024-10-11 11:37:34

在 json 对象数组上使用它

jsonObjectArray.reverse();
$.each(jsonObjectArray, function(i, item) {
    //do something with the item
});

希望这有帮助

Use this on json objects arrays

jsonObjectArray.reverse();
$.each(jsonObjectArray, function(i, item) {
    //do something with the item
});

Hope this helps

苦笑流年记忆 2024-10-11 11:37:34

这可能会有所帮助。将 json 对象中的所有键获取到一个数组中,您可以对其进行排序。

var a = { 1 : 'x', 3 : 'y', 2 : 'z' };
var keys = []
for (i in a) { keys.push(i); }
keys.sort();

然后你可以使用reverse()和slice()来迭代你需要的键。

$.each(keys, function(idx, key) { 
  // do whatever with a[key]
}); 

This might help. Get all the keys from the json objects into an array, which you can sort.

var a = { 1 : 'x', 3 : 'y', 2 : 'z' };
var keys = []
for (i in a) { keys.push(i); }
keys.sort();

then you can use reverse() and slice() to just iterate over the keys you need.

$.each(keys, function(idx, key) { 
  // do whatever with a[key]
}); 
白云不回头 2024-10-11 11:37:34

按照相反的顺序遵循 json,

for(json.length;i>=0;i--)
{
    console.log(json.[i]);
}

Follow the json in reverse order,

for(json.length;i>=0;i--)
{
    console.log(json.[i]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文