如何在 JavaScript 中将单维对象转换为多维对象? [英] How to convert a single dimensional object to a multi-dimensional object in JavaScript?
问题描述
这里我做了一个将多维对象转换为单维对象的代码->
const magic = (obj, parent)=>{for(let key in obj){if(typeof obj[key] === 'object'){魔术(对象[键],父+-"+键);}别的{ans[父级+-";+ 键] = 对象[键];}}}常量对象 = {编号:101,电子邮件:'jack@dev.com',个人信息: {name: '杰克',地址: {line1: 'westwish st',line2: 'washmasher',城市:'沃拉斯',状态:'WX'}}}魔术(对象,对象");
我试图再次将这个一维对象变成一个多维对象,但遇到了许多错误.请帮我写一个函数来将这个一维对象转换成一个多维对象.
转换后的一维对象示例:
<预><代码>{对象 ID: 101 ,目标电子邮件:jack@dev.com",obj-personalInfo-name:杰克",obj-personalInfo-address-line1:westwish st";,obj-personalInfo-address-line2:washmasher";,obj-personalInfo-address-city: wallas",obj-personalInfo-address-state:WX";}我们可以将其构建在一个添加路径的函数之上,例如 ['personalInfo' , 'address', 'city']
和一个对象的关联值,如下所示:
const addPath = ([p, ...ps], val, obj) =>ps .length == 0?{...obj, [p]: val}: {...obj, [p]: addPath (ps, val, obj[p] || {})}
那么我们的逆向魔法函数就很简单了:
const cigam = (obj) =>//魔法"向后对象 .entries (obj).reduce ((a, [k, v]) => addPath (k .split ('-'), v, a), {})
但是,我也认为您的 magic
函数可以稍微清理一下,尤其是当它更改其范围之外的变量 ans
时.我经常写一些 pathEntries
的版本,它可以将你的原始文件转换成类似
[['id', 101] ,['email', 'jack@dev.com'],['personalInfo-name', 'Jack'],['personalInfo-address-line1', 'westwish st'],['personalInfo-address-line2', 'washmasher'],['personalInfo-address-city', 'wallas'],['personalInfo-address-state', 'WX']]
有了这个,我们可以像编写magic
一样简单
const magic = (obj) =>对象 .fromEntries (pathEntries (obj))
这不包括你的 parent
属性,这对我来说似乎是多余的,但如果你想要它,我们会添加一点:
const magic = (obj, parent) =>对象 .fromEntries (pathEntries (obj) .map (([k, v]) => [`${parent}-${k}`, v]))
把这一切放在一起,我们可以做到:
const pathEntries = (obj) =>对象 (obj) === obj?对象 .entries (obj) .flatMap (([k, x]) =>pathEntries (x) .map (([p, v]) => [k + (p ? '-' : '') + p, v])): [['', 对象]]const magic = (obj) =>对象 .fromEntries (pathEntries (obj))const addPath = ([p, ...ps], val, obj) =>ps .length == 0?{...obj, [p]: val}: {...obj, [p]: addPath (ps, val, obj[p] || {})}const cigam = (obj) =>//向后魔法"对象 .entries (obj).reduce ((a, [k, v]) => addPath (k .split ('-'), v, a), {})const obj = {id: 101, email: "jack@dev.com", personalInfo: {name: "Jack", address: {line1: "westwish st", line2: "washmasher", city: "wallas", state:WX"}}}const 魔法 = 魔法 (obj)控制台 .log(附魔)const unenchanted = cigam(附魔)控制台 .log(未附魔)
.as-console-wrapper {max-height: 100% !important;顶部:0}
如果你确实想要 parent
属性,它不会有太大变化:
const pathEntries = (obj) =>对象 (obj) === obj?对象 .entries (obj) .flatMap (([k, x]) =>pathEntries (x) .map (([p, v]) => [k + (p ? '-' : '') + p, v])): [['', 对象]]const magic = (obj, name) =>Object .fromEntries (pathEntries (obj) .map (([k, v]) => [`${name}-${k}`, v]))const addPath = ([p, ...ps], val, obj) =>ps .length == 0?{...obj, [p]: val}: {...obj, [p]: addPath (ps, val, obj[p] || {})}const cigam = (obj) =>//向后魔法"对象 .entries (obj).reduce ((a, [k, v]) => addPath (k .split ('-'), v, a), {})const obj = {id: 101, email: "jack@dev.com", personalInfo: {name: "Jack", address: {line1: "westwish st", line2: "washmasher", city: "wallas", state:WX"}}}const 魔法 = 魔法 (obj, 'obj')控制台 .log(附魔)const unenchanted = cigam(附魔)['obj']控制台 .log(未附魔)
Here I made a code to convert a multi-dimensional object to a single-dimensional object ->
const magic = (obj, parent)=>{
for(let key in obj){
if(typeof obj[key] === 'object')
{
magic(obj[key], parent+"-"+key);
}
else{
ans[parent + "-" + key] = obj[key];
}
}
}
const obj = {
id: 101,
email: 'jack@dev.com',
personalInfo: {
name: 'Jack',
address: {
line1: 'westwish st',
line2: 'washmasher',
city: 'wallas',
state: 'WX'
}
}
}
magic(obj, "obj");
I'm trying to make this 1D object into a Multi-D object again but running into many bugs. Please help me out to write the function to convert this 1D object to a Multi-D object.
Example of the converted 1D object:
{
obj-id: 101 ,
obj-email: "jack@dev.com" ,
obj-personalInfo-name: "Jack" ,
obj-personalInfo-address-line1: "westwish st" ,
obj-personalInfo-address-line2: "washmasher" ,
obj-personalInfo-address-city: "wallas" ,
obj-personalInfo-address-state: "WX"
}
We can build this atop a function that adds a path such as ['personalInfo' , 'address', 'city']
and an associated value to an object, something like this:
const addPath = ([p, ...ps], val, obj) =>
ps .length == 0
? {...obj, [p]: val}
: {...obj, [p]: addPath (ps, val, obj[p] || {})}
Then our reversed magic function is simple:
const cigam = (obj) => // "magic" backwards
Object .entries (obj)
.reduce ((a, [k, v]) => addPath (k .split ('-'), v, a), {})
However, I also think your magic
function could be cleaned up a bit, especially as it's altering a variable outside its scope, ans
. I often write some version of pathEntries
which could convert your original into something like
[
['id', 101] ,
['email', 'jack@dev.com'],
['personalInfo-name', 'Jack'],
['personalInfo-address-line1', 'westwish st'],
['personalInfo-address-line2', 'washmasher'],
['personalInfo-address-city', 'wallas'],
['personalInfo-address-state', 'WX']
]
With that, we can write magic
as simply as
const magic = (obj) =>
Object .fromEntries (pathEntries (obj))
This does not include your parent
property, which seems superfluous to me, but if you wanted it, we would just add a bit:
const magic = (obj, parent) =>
Object .fromEntries (pathEntries (obj) .map (([k, v]) => [`${parent}-${k}`, v]))
Putting this all together, we can do this:
const pathEntries = (obj) =>
Object (obj) === obj
? Object .entries (obj) .flatMap (
([k, x]) => pathEntries (x) .map (([p, v]) => [k + (p ? '-' : '') + p, v])
)
: [['', obj]]
const magic = (obj) =>
Object .fromEntries (pathEntries (obj))
const addPath = ([p, ...ps], val, obj) =>
ps .length == 0
? {...obj, [p]: val}
: {...obj, [p]: addPath (ps, val, obj[p] || {})}
const cigam = (obj) => // "magic" backwards
Object .entries (obj)
.reduce ((a, [k, v]) => addPath (k .split ('-'), v, a), {})
const obj = {id: 101, email: "jack@dev.com", personalInfo: {name: "Jack", address: {line1: "westwish st", line2: "washmasher", city: "wallas", state: "WX"}}}
const enchanted = magic (obj)
console .log (enchanted)
const unenchanted = cigam (enchanted)
console .log (unenchanted)
.as-console-wrapper {max-height: 100% !important; top: 0}
And if you do want that parent
property, it wouldn't change much:
const pathEntries = (obj) =>
Object (obj) === obj
? Object .entries (obj) .flatMap (
([k, x]) => pathEntries (x) .map (([p, v]) => [k + (p ? '-' : '') + p, v])
)
: [['', obj]]
const magic = (obj, name) =>
Object .fromEntries (pathEntries (obj) .map (([k, v]) => [`${name}-${k}`, v]))
const addPath = ([p, ...ps], val, obj) =>
ps .length == 0
? {...obj, [p]: val}
: {...obj, [p]: addPath (ps, val, obj[p] || {})}
const cigam = (obj) => // "magic" backwards
Object .entries (obj)
.reduce ((a, [k, v]) => addPath (k .split ('-'), v, a), {})
const obj = {id: 101, email: "jack@dev.com", personalInfo: {name: "Jack", address: {line1: "westwish st", line2: "washmasher", city: "wallas", state: "WX"}}}
const enchanted = magic (obj, 'obj')
console .log (enchanted)
const unenchanted = cigam (enchanted) ['obj']
console .log (unenchanted)
这篇关于如何在 JavaScript 中将单维对象转换为多维对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!