使用完全限定的字符串设置JSON属性

时间:2022-03-23 23:24:01

This question achieves kinda the opposite of what I'm trying to do. Basically, I have this object:

这个问题和我想做的有点相反。基本上,我有这个对象:

var a = {
    b: {
        c: 'Foo'
    }
}

What I need to do is set the value of c given the string 'b.c'. Unfortunately, I can't do this:

我需要做的是将c的值设为c。不幸的是,我做不到:

a['b.c'] = 'Bar'

As far as I can tell, the question above doesn't get me anywhere close as it just copies the values of the object properties so they can be read. It doesn't help me set the values of the object properties, however. Here's what I have so far:

就我所知,上面的问题并没有让我接近,因为它只是复制对象属性的值,以便读取它们。但是,它不能帮助我设置对象属性的值。这是我目前所拥有的:

var key = 'b.c'.split('.');

for (var i = 0; i < key.length; i++) {
    // do something
}

3 个解决方案

#1


1  

You have to intercept the last iteration of the loop, and from there assign instead of redefining your temp variable.

您必须拦截循环的最后一次迭代,然后从那里分配而不是重新定义临时变量。

I adapted the answer to the question you linked to assign the value 2 to a.b.c:

我将这个问题的答案应用于你将价值2分配给a.b.c的问题:

var a = {
  "b" : {
    "c" : 1
  }
}

var n = "b.c".split(".");
var x = a;
for(var i = 0; i < n.length; i++){
    if(i == n.length-1) {
        x[n[i]] = 2;
    } else {
        x = x[n[i]];
    }
}

http://jsfiddle.net/Fuhbb/

http://jsfiddle.net/Fuhbb/


This is pretty much what @Ian is doing in his jsfiddle. Intead of an if, he loops one step less, then deals with the assignment after the loop.

这就是@Ian在jsfiddle做的事情。在if的Intead中,他少走一步,然后在循环之后处理赋值。

#2


3  

Here's a functional way, is this what you need? It doesn't use the particular a[string] syntax but a function where you can pass the object string and the value to set:

这是一种功能方法,这是你需要的吗?它不使用特定的a[string]语法,而是一个函数,您可以在其中传递对象字符串和要设置的值:

var obj = { foo: { bar: { lol: { lulz: 69 } } } };

function setProp(obj, prop, value) {
  var props = prop.split('.');
  return [obj].concat(props).reduce(function(a,b,i) {
    return i == props.length ? a[b] = value : a[b];
  });
}

setProp(obj, 'foo.bar.lol.lulz', 'hello world');

console.log(obj.foo.bar.lol.lulz); //=> "hello world"

#3


0  

For the record, I eventually figured out another way to do this:

作为记录,我最终想出了另一种方法:

function setProperty(obj, props, val) {
    if (obj[props[0]] instanceof Object) {
        setProperty(obj[props[0]], props.slice(1, props.length), val);

        return;
    }

    obj[props[0]] = val;
}

Call it like this:

叫它是这样的:

a = {
    b: {
        c: 'Foo'
    }
};

var whatever = 'b.c';

var props = whatever.split('.');

setProperty(a, props, 'Bar');

#1


1  

You have to intercept the last iteration of the loop, and from there assign instead of redefining your temp variable.

您必须拦截循环的最后一次迭代,然后从那里分配而不是重新定义临时变量。

I adapted the answer to the question you linked to assign the value 2 to a.b.c:

我将这个问题的答案应用于你将价值2分配给a.b.c的问题:

var a = {
  "b" : {
    "c" : 1
  }
}

var n = "b.c".split(".");
var x = a;
for(var i = 0; i < n.length; i++){
    if(i == n.length-1) {
        x[n[i]] = 2;
    } else {
        x = x[n[i]];
    }
}

http://jsfiddle.net/Fuhbb/

http://jsfiddle.net/Fuhbb/


This is pretty much what @Ian is doing in his jsfiddle. Intead of an if, he loops one step less, then deals with the assignment after the loop.

这就是@Ian在jsfiddle做的事情。在if的Intead中,他少走一步,然后在循环之后处理赋值。

#2


3  

Here's a functional way, is this what you need? It doesn't use the particular a[string] syntax but a function where you can pass the object string and the value to set:

这是一种功能方法,这是你需要的吗?它不使用特定的a[string]语法,而是一个函数,您可以在其中传递对象字符串和要设置的值:

var obj = { foo: { bar: { lol: { lulz: 69 } } } };

function setProp(obj, prop, value) {
  var props = prop.split('.');
  return [obj].concat(props).reduce(function(a,b,i) {
    return i == props.length ? a[b] = value : a[b];
  });
}

setProp(obj, 'foo.bar.lol.lulz', 'hello world');

console.log(obj.foo.bar.lol.lulz); //=> "hello world"

#3


0  

For the record, I eventually figured out another way to do this:

作为记录,我最终想出了另一种方法:

function setProperty(obj, props, val) {
    if (obj[props[0]] instanceof Object) {
        setProperty(obj[props[0]], props.slice(1, props.length), val);

        return;
    }

    obj[props[0]] = val;
}

Call it like this:

叫它是这样的:

a = {
    b: {
        c: 'Foo'
    }
};

var whatever = 'b.c';

var props = whatever.split('.');

setProperty(a, props, 'Bar');