Javascript:如何从点分隔的字符串创建对象? [英] Javascript: How to create an object from a dot separated string?
问题描述
我遇到了这个潜在的场景,我向我的一些员工提出了一个测试问题.我可以想到几种方法来解决这个问题,但它们都不是很漂亮.我想知道什么解决方案可能最适合这个以及任何优化技巧.问题来了:
I ran into this potential scenario that I posed to a few of my employees as a test question. I can think of a couple ways to solve this problem, but neither of them are very pretty. I was wondering what solutions might be best for this as well as any optimization tips. Here's the question:
给定一些任意长度的点符号中的任意字符串mystr"(例如mystr =node1.node2.node3.node4"),编写一个名为expand"的函数,它将创建这些项目中的每一个作为一个新的节点层在 js 对象中.对于上面的示例,它应该输出以下内容,因为我的对象名称是blah":
Given some arbitrary string "mystr" in dot notation (e.g. mystr = "node1.node2.node3.node4") at any length, write a function called "expand" that will create each of these items as a new node layer in a js object. For the example above, it should output the following, given that my object name is "blah":
blah: { node1: { node2: { node3: { node4: {}}}}}
来自函数调用:
mystr = "node1.node2.node3.node4";
blah = {};
expand(blah,mystr);
或者,如果更简单,可以创建函数来将变量设置为返回值:
Alternately, if easier, the function could be created to set a variable as a returned value:
mystr = "node1.node2.node3.node4";
blah = expand(mystr);
额外的功劳:有一个可选的函数参数,用于设置最后一个节点的值.所以,如果我调用我的函数expand"并像这样调用它:expand(blah, mystr, "value"),输出应该和以前一样,但是用 node4 = "value" 而不是 {}.
Extra credit: have an optional function parameter that will set the value of the last node. So, if I called my function "expand" and called it like so: expand(blah, mystr, "value"), the output should give the same as before but with node4 = "value" instead of {}.
推荐答案
这是我脑海中突然出现的一个方法.它使用点符号分割字符串,然后循环遍历节点以在对象内部创建对象,使用移位引用"(但不确定这是否是正确的术语).
Here's a method that popped up in my mind. It splits the string on the dot notation, and then loops through the nodes to create objects inside of objects, using a 'shifting reference' (not sure if that's the right term though).
函数内的对象 output
包含在整个函数中构建的完整对象,但 ref
在 output<中保留了一个越来越深的引用/code>,因为在 for 循环中创建了新的子对象.
The object output
within the function contains the full object being built throughout the function, but ref
keeps a reference that shifts to deeper and deeper within output
, as new sub-objects are created in the for-loop.
最后,最后一个值应用于最后一个给定的名称.
Finally, the last value is applied to the last given name.
function expand(str, value)
{
var items = mystr.split(".") // split on dot notation
var output = {} // prepare an empty object, to fill later
var ref = output // keep a reference of the new object
// loop through all nodes, except the last one
for(var i = 0; i < items.length - 1; i ++)
{
ref[items[i]] = {} // create a new element inside the reference
ref = ref[items[i]] // shift the reference to the newly created object
}
ref[items[items.length - 1]] = value // apply the final value
return output // return the full object
}
然后返回对象,因此可以使用此表示法:
The object is then returned, so this notation can be used:
mystr = "node1.node2.node3.node4";
blah = expand(mystr, "lastvalue");
这篇关于Javascript:如何从点分隔的字符串创建对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!