[英]Javascript strange behavior in Json Object Manipulation
I really can't get my hand around what is happening there and I really think that I should share this with you guys: 我真的无法掌握那里发生的一切,我真的认为我应该与你们分享:
I am making a call to my api at '/products' and the response looks something like this in postman: 我正在'/ products'上调用我的api,响应在邮递员中看起来像这样:
[
{
"id": 2,
"name": "some_product_name",
"description": "description",
"price": "$120.00",
"firmware_version": "1.0",
"quantity_in_stock": 50,
"selling": true,
"build_version": "1.2",
"category_id": 1,
"available_colors": [
{
"name": "blue",
"in_stock": true,
"picture": {
"type": "Buffer",
"data": []
}
},
{
"name": "black",
"in_stock": true,
"picture": null
},
{
"name": "silver",
"in_stock": true,
"picture": {
"type": "Buffer",
"data": []
}
}
]
}
]
Now what I am trying to do here is to create a new object called products_showcase that has one entry per product color, with the same informations except for the available_colors property, replaced by the color object: 现在,我要在这里创建一个名为products_showcase的新对象,该对象对每种产品颜色都有一个条目,除了可用颜色属性之外,其他信息都相同,由颜色对象代替:
$scope.initModel = function() {
$http({
method: 'GET',
url: '/products'
}).then(function(resp) {
console.log(resp.data);
$scope.products = resp.data;
$scope.products.forEach((item, index, array) => {
item.available_colors.forEach((color, index, array) => {
var product = item;
product.color = {};
product.color = color;
delete product['available_colors'];
$scope.products_showcase.push(product);
});
});
}, function(error) {
$scope.error = error;
});
};
But then, something really strange is happening: 但是随后,发生了一件非常奇怪的事情:
Javascript won't create new object when you assign it to a variable. 将JavaScript分配给变量时,它不会创建新对象。 You can use Object.create function to create a new object from existing one. 您可以使用Object.create函数从现有对象创建一个新对象。
$scope.initModel = function() {
$http({
method: 'GET',
url: '/products'
}).then(function(resp) {
console.log(resp.data);
$scope.products = Object.create(resp.data);
$scope.products.forEach((item, index, array) => {
item.available_colors.forEach((color, index, array) => {
var product = Object.create(item);
product.color = {};
product.color = color;
delete product['available_colors'];
$scope.products_showcase.push(product);
});
});
}, function(error) {
$scope.error = error;
});
};
Javascript assignment operator doesn't apparently create a new instance of the object for the new variable, it simply creates a pointer to the memory address of the 'father' object. Javascript赋值运算符显然不会为新变量创建对象的新实例,而只是创建指向“父亲”对象的内存地址的指针。
Example code: 示例代码:
var object = {property: 'myProp'};
console.log(object);
{property: 'myProp'} {属性:“ myProp”}
var newObj = object;
newObj.newProperty = 'myNewProp';
console.log(object);
{property: 'myProp', newProperty: 'myNewProp'} {属性:“ myProp”,新属性:“ myNewProp”}
To create a new instance of the object we have to use the Object.create() method. 要创建对象的新实例,我们必须使用Object.create()方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.