如何在对象中设置值
How to set value in Object
我必须在对象中设置值。
这是问题所在:当我尝试在具有某些道具的对象中编写道具时,以前的道具将被删除。
我解决了两个案例,这是最后一个。
我需要保存 test2。
我该如何解决?
谢谢。
var obj = {
keyOne: "foo",
keyTwo: {
test1: "baz",
test2: {
test21: ["bar"],
}
}
}
function setObjectProperty(obj, string, value) {
var path = string.split('.');
var currentObj = obj;
for (var i = 0; i < path.length - 1; i++) {
if (!currentObj[path[i]] || currentObj[path[i]] != "string") {
currentObj[path[i]] = {};
currentObj = currentObj[path[i]];
}
}
currentObj[path[path.length - 1]] = value;
};
setObjectProperty(obj, 'keyOne', 'new');
setObjectProperty(obj, 'keyOne.key.key2', 'newnew');
setObjectProperty(obj, 'keyTwo.test1', 'zzz');
console.log(obj);
测试 currentObj[path[i]] != "string"
看起来很奇怪,我想你想用 typeof
代替。
在你的循环中,你应该在每一步都前进,但只创建一个不存在的对象。
工作代码:
function setObjectProperty(obj, string, value) {
var path = string.split('.');
var currentObj = obj;
for (var i = 0; i < path.length - 1; i++) {
if (!currentObj[path[i]] || typeof currentObj[path[i]] == "string") {
currentObj[path[i]] = {};
}
currentObj = currentObj[path[i]];
}
currentObj[path[path.length - 1]] = value;
};
进行了少量更正。
- 无论创建子对象都需要推进对象路径
- 检查字符串类型,需要使用
typeof
var obj = {
keyOne: "foo",
keyTwo: {
test1: "baz",
test2: {
test21: ["bar"],
}
}
}
function setObjectProperty(obj, string, value) {
var path = string.split('.');
var currentObj = obj;
for (var i = 0; i < path.length - 1; i++) {
if (!currentObj[path[i]] || typeof currentObj[path[i]] === 'string') {
currentObj[path[i]] = {};
}
currentObj = currentObj[path[i]];
}
currentObj[path[path.length - 1]] = value;
};
setObjectProperty(obj, 'keyOne', 'new');
setObjectProperty(obj, 'keyOne.key.key2', 'newnew');
setObjectProperty(obj, 'keyTwo.test1', 'zzz');
console.log(obj);
这是您要找的吗?
const store = {
keyOne: "foo",
keyTwo: {
test1: "baz",
test2: {
test21: ["bar"],
}
}
}
function getKeys(key) {
return key.split(".").filter(k => k.length);
}
function assignProps(object, keys, value = null, root = null) {
root = root || object;
const key = keys.shift();
if (!key) return root;
object[key] = keys.length === 0 ? value : {};
assignProps(object[key], keys, value, root);
}
assignProps(store, getKeys("keyOne.foo.bar"), "baz");
assignProps(store, getKeys("keyTwo.test1.test2"), "value");
console.log(store);
我必须在对象中设置值。
这是问题所在:当我尝试在具有某些道具的对象中编写道具时,以前的道具将被删除。
我解决了两个案例,这是最后一个。 我需要保存 test2。 我该如何解决?
谢谢。
var obj = {
keyOne: "foo",
keyTwo: {
test1: "baz",
test2: {
test21: ["bar"],
}
}
}
function setObjectProperty(obj, string, value) {
var path = string.split('.');
var currentObj = obj;
for (var i = 0; i < path.length - 1; i++) {
if (!currentObj[path[i]] || currentObj[path[i]] != "string") {
currentObj[path[i]] = {};
currentObj = currentObj[path[i]];
}
}
currentObj[path[path.length - 1]] = value;
};
setObjectProperty(obj, 'keyOne', 'new');
setObjectProperty(obj, 'keyOne.key.key2', 'newnew');
setObjectProperty(obj, 'keyTwo.test1', 'zzz');
console.log(obj);
测试 currentObj[path[i]] != "string"
看起来很奇怪,我想你想用 typeof
代替。
在你的循环中,你应该在每一步都前进,但只创建一个不存在的对象。
工作代码:
function setObjectProperty(obj, string, value) {
var path = string.split('.');
var currentObj = obj;
for (var i = 0; i < path.length - 1; i++) {
if (!currentObj[path[i]] || typeof currentObj[path[i]] == "string") {
currentObj[path[i]] = {};
}
currentObj = currentObj[path[i]];
}
currentObj[path[path.length - 1]] = value;
};
进行了少量更正。
- 无论创建子对象都需要推进对象路径
- 检查字符串类型,需要使用
typeof
var obj = {
keyOne: "foo",
keyTwo: {
test1: "baz",
test2: {
test21: ["bar"],
}
}
}
function setObjectProperty(obj, string, value) {
var path = string.split('.');
var currentObj = obj;
for (var i = 0; i < path.length - 1; i++) {
if (!currentObj[path[i]] || typeof currentObj[path[i]] === 'string') {
currentObj[path[i]] = {};
}
currentObj = currentObj[path[i]];
}
currentObj[path[path.length - 1]] = value;
};
setObjectProperty(obj, 'keyOne', 'new');
setObjectProperty(obj, 'keyOne.key.key2', 'newnew');
setObjectProperty(obj, 'keyTwo.test1', 'zzz');
console.log(obj);
这是您要找的吗?
const store = {
keyOne: "foo",
keyTwo: {
test1: "baz",
test2: {
test21: ["bar"],
}
}
}
function getKeys(key) {
return key.split(".").filter(k => k.length);
}
function assignProps(object, keys, value = null, root = null) {
root = root || object;
const key = keys.shift();
if (!key) return root;
object[key] = keys.length === 0 ? value : {};
assignProps(object[key], keys, value, root);
}
assignProps(store, getKeys("keyOne.foo.bar"), "baz");
assignProps(store, getKeys("keyTwo.test1.test2"), "value");
console.log(store);