Javascript 在 JSON 对象的顶部添加一些东西

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10773813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:52:07  来源:igfitidea点击:

Adding something to the top of a JSON object

javascriptjqueryjson

提问by Click Upvote

I have a JSON object which is initiated when the page is loaded, like this:

我有一个 JSON 对象,它在页面加载时启动,如下所示:

data[foo] = bar;
data[foo2] = bar2;
data[foo3] = bar3;

Is there a way to inject an element before the first fooelement, so that when doing a for var i in data, the new element will be looped through before the elements that were added when the object was initiated?

有没有办法在第一个元素之前注入一个元素foo,以便在执行 a 时for var i in data,新元素将在对象启动时添加的元素之前循环?

The reason is, I'm displaying some items to the user. When the user adds a new item via javascript, I want this new item to be displayed above all the existing items, however when I add the new item, i.e

原因是,我正在向用户显示一些项目。当用户通过 javascript 添加新项目时,我希望这个新项目显示在所有现有项目的上方,但是当我添加新项目时,即

data[newItem] = newItem;

Then the JSON object looks like this:

然后 JSON 对象如下所示:

data[foo] = bar;
data[foo2] = bar2;
data[foo3] = bar3;
data[newItem] = newItem;

Instead of how I want, which is:

而不是我想要的方式,即:

data[newItem] = newItem;
data[foo] = bar;
data[foo2] = bar2;
data[foo3] = bar3;

Any ideas?

有任何想法吗?

回答by Joseph

In JS, object properties' order is not guaranteed. Therefore, even if they are ordered in the JSON string, when parsed as a JS object, you will never predict in what order they come up.

在 JS 中,不保证对象属性的顺序。因此,即使它们在 JSON 字符串中排序,当解析为 JS 对象时,您也永远无法预测它们以什么顺序出现。

Better use arrays instead. You could use the unshift()method to put the item in the first index.

最好使用数组代替。您可以使用该unshift()方法将项目放在第一个索引中。

var data = [bar,bar2,bar3];

data.unshift(newItem);

//data = [newItem,bar,bar2,bar3];

回答by Click Upvote

As a compliment to Joseph the Dreamer's answer, I have ran some quick checks in firefox & chrome.

作为一种恭维约瑟夫·梦想家答案,我已经跑在Firefox和Chrome的一些快速检查。

Firefox:

火狐:

var obj = {};

obj.a = 'a';
obj.c = 'c';
obj.b = 'b';
obj['0'] = '0';

for(var i in obj){
    console.log(i);
}

//prints:
a
c
b
0

Chrome:

铬合金:

var obj = {};

obj.a = 'a';
obj.c = 'c';
obj.b = 'b';
obj['0'] = '0';

for(var i in obj){
    console.log(i);
}

//prints:
0
a
c
b

回答by raubas

I've stumbled upon this and achieved it using:

我偶然发现了这一点并使用以下方法实现了它:

const newObject = Object.assign({first: value}, oldObject)

const newObject = Object.assign({first: value}, oldObject)

As mentioned, order is not guaranteed but for me this is good enough. :)

如前所述,订单不能保证,但对我来说这已经足够了。:)

回答by Incognito

Is there a way to inject an element before the first foo element?

有没有办法在第一个 foo 元素之前注入一个元素?

Which comes first in the array:

哪个是数组中的第一个:

window.object or window.alert?

window.object 或 window.alert?

Neither, objects don't have an order. If you want an array use an array. Objects are not arrays.

两者都没有,对象没有顺序。如果你想要一个数组,请使用一个数组。对象不是数组。

If you want

如果你想

var ThingsInOrder = [
  FirstThing,
  SecondThing,
  ThirdThing
];
ThingsInOrder.push(ForthThing);

Use an array.

使用数组。

If you want:

如果你想:

var ThingsNeverInOrder = {
  Window,
  Alert,
  Derp,
  Herp
};

ThingsNeverInOrder.foo = bar;

Use an object.

使用一个对象。

回答by virender nehra

Instead of adding new value in the same object, you can create a new object and arrange properties order as you want. example :

您可以创建一个新对象并根据需要排列属性顺序,而不是在同一对象中添加新值。例子 :

var objj1 = {name:'viru',lastname:'nehra'};

So create a new object with new property which you want on top:

因此,创建一个具有您想要的新属性的新对象:

var obj2 = {age: 21}

and the run:

和运行:

for(var key in objj1){
   obj2[key] = objj1[key]
}

obj2 = {age: 21, name: "viru", lastname: "nehra"}

回答by Samir Adel

I think you can convert it to string, append your data at the beginning of the string then re-convert the string to json using "eval"

我认为您可以将其转换为字符串,在字符串的开头附加您的数据,然后使用“eval”将字符串重新转换为 json