我有一个像这样的对象
{"first":["first 1"],"second":["second 1","second 2"],"third":["third 1", "third 2"]}
我希望能够根据变量值更改属性名称。 如果变量是"第二个"它应该将属性改为我想要的任何东西。 我尝试了一些方法......
这个
object[theVariable] = "new second";
会将属性的值更改为此
{"first":["first 1"],"second":"new second","third":["third 1", "third 2"]}
这个
object.theVariable = "new second";
将创建一个像这样的新属性
{"first":["first 1"],"second":["second 1","second 2"],"third":["third 1", "third 2"],"theVariable":"new second"}
这些方法都不会改变属性" second"到"新的第二"什么时候"秒"存储在变量" theVariable"
中期望的结果:
{"first":["first 1"],"new second":["second 1","second 2"],"third":["third 1", "third 2"]}
如何做到这一点?
答案 0 :(得分:1)
如果我理解正确,您想要更改对象中属性的名称。这应该有用。
double TotalEarnings = 0.0d;
_SortedList.AsParallel().WithDegreeOfParallelism(5).ForAll(item => {
TotalEarnings += Convert.ToDouble(item.SaleEarning);
});
答案 1 :(得分:1)
var obj = {"first":["first 1"],"second":["second 1","second 2"],"third":["third 1", "third 2"]};
var theVariable = "new second";
obj[theVariable] = obj.second;
delete obj.second;
console.log(obj);

答案 2 :(得分:-2)
通过括号表示法使用属性访问:
var obj = {
"first":["first 1"],
"second":["second 1","second 2"],
"third":["third 1", "third 2"]
};
console.log(obj);
var propNameVariable = "second";
obj[propNameVariable] = "new second";
console.log(obj);