复制 Javascript 对象属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6620296/
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
Copying Javascript object attributes
提问by Shawn Mclean
I have 1 object coming from the server with multiple properties in which I want to hydrate it into a new object, changing the name of 1 property and keeping the rest.
我有 1 个来自服务器的对象,它具有多个属性,我想将其合并为一个新对象,更改 1 个属性的名称并保留其余属性。
Code:
代码:
JSON: { UserId: 1, Name: "Woo", Age: 10 }
JSON: { UserId: 1, Name: "Woo", Age: 10 }
The format of the object I want it in:
我想要它的对象的格式:
var newObj = {}
newObj.id = jsonObj.UserId;
//Everything property below here is the same. How can i prevent writing this code?
newObj.Name = jsonObj.Name;
newObj.Age = jsonObj.Age;
What I'm doing is based on this answer, trying to parse some json into a format that requires me to change the name of 1 property.
我正在做的是基于此答案,尝试将一些 json 解析为需要我更改 1 属性名称的格式。
回答by aroth
For such a simple case, you could do something like:
对于这样一个简单的情况,您可以执行以下操作:
var newObj = {id: jsonObj.UserId, Name: jsonObj.Name, Age: jsonObj.Age};
For a more complex object with a large number of fields, you might prefer something like:
对于具有大量字段的更复杂的对象,您可能更喜欢以下内容:
//helper function to clone a given object instance
function copyObject(obj) {
var newObj = {};
for (var key in obj) {
//copy all the fields
newObj[key] = obj[key];
}
return newObj;
}
//now manually make any desired modifications
var newObj = copyObject(jsonObj);
newObj.id = newObj.UserId;
回答by Ronan Quillevere
If you want to copy only specific fields
如果您只想复制特定字段
/**
* Returns a new object with only specified fields copied.
*
* @param {Object} original object to copy fields from
* @param {Array} list of fields names to copy in the new object
* @return {Object} a new object with only specified fields copied
*/
var copyObjectFields = function (originObject, fieldNamesArray)
{
var obj = {};
if (fieldNamesArray === null)
return obj;
for (var i = 0; i < fieldNamesArray.length; i++) {
obj[fieldNamesArray[i]] = originObject[fieldNamesArray[i]];
}
return obj;
};
//example of method call
var newObj = copyObjectFields (originalObject, ['field1','field2']);
回答by chachan
I mostly prefer to reuse instead of recreate so I'd suggest http://underscorejs.org/#clone
我最喜欢重用而不是重新创建所以我建议http://underscorejs.org/#clone
回答by Benny Tjia
Dont really understand your question, but this is what I normally do when I extract from an existing object:
不太明白您的问题,但这是我从现有对象中提取时通常会做的事情:
var newObj = new Object(jsonObj);
alert(newObj.UserId === jsonObj.UserId); //returns true
Is that what you were asking? Hope that helps.
这就是你问的吗?希望有帮助。
回答by Evgeniy Timchenko
function clone(o) {
if(!o || 'object' !== typeof o) {
return o;
}
var c = 'function' === typeof o.pop ? [] : {};
var p, v;
for(p in o) {
if(o.hasOwnProperty(p)) {
v = o[p];
if(v && 'object' === typeof v) {
c[p] = clone(v);
}
else {
c[p] = v;
}
}
}
return c;
}
回答by Tony
Using Object.assign
使用 Object.assign
var newObj = Object.assign({}, jsonObj);
Using JSON.parse and JSON.stringify;
使用 JSON.parse 和 JSON.stringify;
var newObj = JSON.parse(JSON.stringify(jsonObj));