[英]Convert an object into another type of object in JavaScript
I want to convert the object which I am getting from a function into the NoSql database format.我想将从函数中获取的对象转换为 NoSql 数据库格式。
I have an object like this:我有一个这样的对象:
{
"visible": true,
"active": true,
"invalidated": true,
"handles": {
"start": {
"x": 2094.803738317757,
"y": 2038.5794392523367,
"highlight": true,
"active": false
},
"end": {
"x": 2487.1775700934586,
"y": 2555.364485981309,
"highlight": true,
"active": false,
"moving": false
},
"initialRotation": 0,
}
}
I created the NoSQL databases like the below one and now want to convert the above object into an object like this:我创建了如下所示的 NoSQL 数据库,现在想将上面的对象转换为这样的对象:
"visible": {
"BOOL": true
},
"active": {
"BOOL": true
},
"handles": {
"M": {
"end": {
"M": {
"active": {
"BOOL": false
},
"highlight": {
"BOOL": true
},
"moving": {
"BOOL": false
},
"x": {
"N": "2487"
},
"y": {
"N": "2555"
}
}
},
"initialRotation": {
"N": "0"
},
"start": {
"M": {
"active": {
"BOOL": false
},
"highlight": {
"BOOL": true
},
"x": {
"N": "2094"
},
"y": {
"N": "2038"
}
}
}
}
}
I cannot find a way to solve this problem.我找不到解决这个问题的方法。
Is there anything wrong with the simplest function, in that use case?在那个用例中,最简单的函数有什么问题吗? I made some assumptions based of the object you provided as an example: that you are flooring the coordinate numbers, and you want them as a string in the new object.
我根据您提供的对象作为示例做出了一些假设:您正在对坐标编号进行地板化,并且您希望它们作为新对象中的字符串。
const convertObj = (obj) => {
return {
visible: {
BOOL: obj.visible,
},
active: {
BOOL: obj.active,
},
handles: {
M: {
end: {
M: {
active: {
BOOL: obj.handles.end.active,
},
highlight: {
BOOL: obj.handles.end.highlight,
},
moving: {
BOOL: obj.handles.end.moving,
},
x: {
N: Math.floor(obj.handles.end.x).toString(),
},
y: {
N: Math.floor(obj.handles.end.y).toString(),
},
},
},
initialRotation: {
N: obj.handles.initialRotation.toString(),
},
start: {
M: {
active: {
BOOL: obj.handles.start.active,
},
highlight: {
BOOL: obj.handles.start.highlight,
},
x: {
N: Math.floor(obj.handles.start.x).toString(),
},
y: {
N: Math.floor(obj.handles.start.y).toString(),
},
},
},
},
},
};
}; };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.