您好我以下面的方式将JSON作为对象:
{
"id": "001724",
"select": false,
"class": "product",
"Names": [
"A",
"B",
"C"
],
"available": true,
"description": "selected product",
}
我想在我的javascript文件中使用Underscore.js对我的JSON进行排序,我正在尝试将我的主对象克隆为“命名”对象,添加一个属性“name”
[{
A:[
0:{
"name":"A"
"id": "001724",
"select": false,
"class": "product",
"available": true,
"description": "selected product"
}
]
B:[
0:{
"name":"B"
"id": "001724",
"select": false,
"class": "product",
"available": true,
"description": "selected product"
}
]
C:[
0:{
"name":"C"
"id": "001724",
"select": false,
"class": "product",
"available": true,
"description": "selected product"
}
]
default:[
0:{
"name":"B"
"id": "001724",
"select": false,
"class": "product",
"available": true,
"description": "selected product"
}
]
}]
,默认数组包含与'B'数组
相同我正在尝试使用underscore.js
来实现这一目标答案 0 :(得分:1)
你试过了吗?
var newObj = _(obj).clone();
答案 1 :(得分:0)
您可以使用Object.create
if(!Object.create){
Object.create = function(o){
function F(){}; F.prototype = o;
return new F;
}
}
var clone = Object.create(yourObjectHere);
var anotherClone = Object.create(yourObjectHere);
如果你不克隆你真正指的是原始对象,即使变量不同。