javascript 在 JSON 的开头添加新元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10691409/
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
add new element at the beginning of a JSON
提问by lito
I have this JSON:
我有这个 JSON:
var myVar = {
"9":"Automotive & Industrial",
"1":"Books",
"7":"Clothing"
};
I want to add a new element at the beginning, I want to end up having this:
我想在开头添加一个新元素,我想最终得到这个:
var myVar = { "5":"Electronics", "9":"Automotive & Industrial", "1":"Books", "7":"Clothing" };
var myVar = { "5":"Electronics", "9":"Automotive & Industrial", "1":"Books", "7":"Clothing" };
I tried this but it is not working:
我试过这个,但它不起作用:
myVar.unshift({"5":"Electronics"});
Thanks!
谢谢!
采纳答案by Jonathan M
Just do it this way:
只需这样做:
var myVar = {
"9":"Automotive & Industrial",
"1":"Books",
"7":"Clothing"
};
// if you want to add a property, then...
myVar["5"]="Electronics"; // note that it won't be "first" or "last", it's just "5"
回答by Ryan O'Donnell
Javascript objects don't have an order associated with them by definition, so this is impossible.
根据定义,Javascript 对象没有与它们关联的顺序,因此这是不可能的。
You should use an array of objectsif you need an order:
如果您需要订单,您应该使用一组对象:
var myArray = [
{number: '9', value:'Automotive & Industrial'},
{number: '1', value:'Books'},
{number: '7', value:'Clothing'}
]
then if you want to insert something in the first position, you can use the unshift method of Arrays.
那么如果你想在第一个位置插入一些东西,你可以使用Arrays的unshift方法。
myArray.unshift({number:'5', value:'Electronics'})
//myArray is now the following
[{number:'5', value:'Electronics'},
{number: '9', value:'Automotive & Industrial'},
{number: '1', value:'Books'},
{number: '7', value:'Clothing'}]
more detail here: Does JavaScript Guarantee Object Property Order?
这里有更多详细信息: JavaScript 是否保证对象属性顺序?
回答by epascarello
There is no way to add new entries to JSON and control the position of them.
无法向 JSON 添加新条目并控制它们的位置。
If you want to deal with order [strange since objects are unordered], you would have to basically create a new object and append the data.
如果你想处理顺序[奇怪,因为对象是无序的],你基本上必须创建一个新对象并附加数据。
回答by Michael Berkowski
Javascript object properties are not deterministically ordered. Properties are accessible no matter what order they're in. If you are depending on them to be in a specific order for a for-in
loop, I would suggest rethinking your strategy. Different browsers will take different approaches to ordering the properties. While many browsers will in fact process them in the order you created them, that behavior isn't actually defined in the language spec so you cannot rely on it.
Javascript 对象属性不是确定性排序的。无论属性的顺序如何,都可以访问它们。如果您依赖它们在for-in
循环中的特定顺序,我建议重新考虑您的策略。不同的浏览器将采用不同的方法对属性进行排序。虽然许多浏览器实际上会按照您创建它们的顺序处理它们,但该行为实际上并未在语言规范中定义,因此您不能依赖它。
回答by E. Maggini
I think the unshift issue might come from the fact that you an associative array. Definition of associative array
我认为 unshift 问题可能来自于你是一个关联数组的事实。 关联数组的定义
This might be a hack but if you MUST have that value at the start of the JSON object, how about:
这可能是一个 hack 但如果您必须在 JSON 对象的开头具有该值,那么如何:
var myNewVar={'5':'Electronics'};
for (xx in myVar) {
myNewVar[xx]=myVar[xx];
}
回答by bbeny
This Can be done using the lodashmerge function like so:
var myObj = _.merge({ col1: 'col 1', col2: 'col 2'}, { col3: 'col 3', col4: 'col 4' });
Your final object will look like this:
您的最终对象将如下所示:
{ col1: 'col 1', col2: 'col 2', col3: 'col 3', col4: 'col 4' }
As others mentioned, there is no guarantee that the order of the keys in the object will remain the same, depending on what you do with it. But, if you perform the merge as your final step, you should be ok. Note, the 'merge' function will produce a completely new object, it will not alter either of the objects you pass into it.
正如其他人所提到的,不能保证对象中键的顺序将保持不变,这取决于您对其进行的操作。但是,如果您将合并作为最后一步执行,则应该没问题。请注意,'merge' 函数将生成一个全新的对象,它不会改变您传递给它的任何一个对象。
回答by Valeriane
Try something like that:
尝试这样的事情:
myObject.sort(function(a,b) { return parseFloat(a.price) - parseFloat(b.price) } );
or
或者
myObject.sort(function(a,b) { return parseFloat(a.get("price")) - parseFloat(b.get("price")) } );