[英]How to clone the same object to three objects using underscore.js
Hi i get my JSON as an object in the below way: 嗨,我通过以下方式将JSON作为对象获取:
{
"id": "001724",
"select": false,
"class": "product",
"Names": [
"A",
"B",
"C"
],
"available": true,
"description": "selected product",
}
And i want to sort my JSON using Underscore.js in my javascript file, i'm trying to clone my main object to "names" objects adding an atrribute "name" 我想在我的javascript文件中使用Underscore.js对JSON进行排序,我正在尝试将我的主对象克隆为“名称”对象,并添加一个粗体的“名称”
[{
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"
}
]
}]
and default array contains same as 'B' array 并且默认数组包含与“ B”数组相同的数组
I'm trying to to achieve this using underscore.js 我正在尝试使用underscore.js实现此目标
您是否尝试过:
var newObj = _(obj).clone();
You can clone an Object in plain JavaScript using Object.create
: 您可以使用
Object.create
在纯JavaScript中克隆对象:
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);
If you don't clone you're really referring to the original Object, even if the variables are different. 如果不克隆,则实际上是在指原始对象,即使变量不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.