javascript 在javascript中复制关联数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10270711/
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
Copy associative array in javascript?
提问by Naveen
I've the following piece of code for copying one associative array to other,
我有以下一段代码用于将一个关联数组复制到另一个,
<script>
var some_db = new Array();
some_db["One"] = "1";
some_db["Two"] = "2";
some_db["Three"] = "3";
var copy_db = new Array();
alert(some_db["One"]);
copy_db = some_db.slice();
alert(copy_db["One"]);
</script>
But the second alert says "undefined".. Am I doing something wrong here? Any pointers please..
但是第二个警报说“未定义”..我在这里做错了什么吗?请任何指针..
回答by iambriansreed
In JavaScript associative arrays are called objects.
在 JavaScript 中,关联数组称为对象。
<script>
var some_db = {
"One" : "1",
"Two" : "2",
"Three" : "3"
};
var copy_db = clone(some_db);
alert(some_db["One"]);
alert(copy_db["One"]);
function clone(obj) {
if (null == obj || "object" != typeof obj) return obj;
var copy = obj.constructor();
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr]);
}
return copy;
}
</script>
I would normally use var copy_db = $.extend({}, some_db);
if I was using jQuery.
var copy_db = $.extend({}, some_db);
如果我使用 jQuery,我通常会使用。
Fiddle Proof:http://jsfiddle.net/RNF5T/
小提琴证明:http : //jsfiddle.net/RNF5T/
Thanks @maja.
谢谢@maja。
回答by gnarf
As @Niko says in the comment, there are no associative arrays in JavaScript.
正如@Niko 在评论中所说,JavaScript 中没有关联数组。
You are actually setting properties on the array object, which is not a very good idea. You would be better off using an actual object.
您实际上是在数组对象上设置属性,这不是一个好主意。最好使用实际对象。
var some_db = {};
some_db["One"] = "1";
some_db["Two"] = "2";
some_db["Three"] = "3";
var copy_db = {}, prop;
// loop over all the keys in the object
for ( prop in some_db ) {
// make sure the object has this value, and not its prototype
if ( some_db.hasOwnProperty( prop ) ) {
copy_db[ prop ] = some_db[ prop ];
}
}
Many libraries implement an extend
function which does exactly this ( copy keys from one object to another). Most notably jQueryand underscore.js. Underscore also has _.clone( obj )
which is effectively _.extend( {}, obj )
许多库实现了一个extend
功能,它正是这样做的(将键从一个对象复制到另一个对象)。最值得注意的是jQuery和underscore.js。下划线也_.clone( obj )
有效_.extend( {}, obj )
回答by 2strokeMotor
if you want to use JSON, you can take this 'associative array' object:
如果你想使用 JSON,你可以使用这个“关联数组”对象:
var assArray = {zero:0,one:1,two:2,three:3,what:'ever',you:'want'};
var assArray = {zero:0,one:1,two:2,three:3,what:'ever',you:'want'};
and 'clone' it like this:
并像这样“克隆”它:
var clonedObj = JSON.parse(JSON.stringify(assArray));
var clonedObj = JSON.parse(JSON.stringify(assArray));
回答by jichi
underscore.clone (http://underscorejs.org/#clone) could help. It performs shallow copy to the dictionary object or array.
underscore.clone ( http://underscorejs.org/#clone) 可以提供帮助。它对字典对象或数组执行浅拷贝。
var some_db = {
"One" : "1",
"Two" : "2",
"Three" : "3"
};
copy_db = _.clone(some_db);