我有一个多级对象,其中包含不同对象的定义。其中一些属性'叶子'是默认值并包含默认值。
我需要在javascript中构建另一个对象,使用相同的路径'到叶子但是使用默认值作为属性的值。
同一楼层的所有物品都可以。用简单的on对象和hasOwnProperty。但在这里,在我看来,我需要更多的时间来探索这个对象,而且我不知道如何解决这个问题,因为我不能使用类似对象的东西[iter1 .iter2.iter3 ...]。 我需要提一下,我正在使用Eureka版本在Service Now服务器上工作,所以它只支持ECMAScript 3.
有一个例子:
objet我需要处理(json编码):
"schema": {"properties": {
"videoCapability": {
"default": "1",
"type": [
"string"
],
"choices": [
{
"value": "0",
"title": "Disabled"
},
{
"value": "1",
"title": "Enabled"
}
], },
"ice": {
"properties": {
"iceCapability": {
"default": "1",
"type": [
"string"
],
"choices": [
{
"value": "0",
"title": "Disabled"
},
{
"value": "1",
"title": "Enabled"
}
],
"name": "iceCapability","
},
"defaultcandidatetype": {
"default": "0",
"type": [
"string"
],
"choices": [
{
"value": "0",
"title": "Host"
},
{
"value": "1",
"title": "Server Reflexive"
},
{
"value": "2",
"title": "Relayed"
}
],
},
etc.
我需要构建的对象:
"config": { config.properties.videoCapability = "1"
config.properties.ice.properties.iceCapability = "1"
config.properties.ice.properties.defaultcandidatetype = "0"
}
等
如果你想知道为什么我需要这样做,那就是第一个对象是从远程服务器的json中检索出来的。在将此json编码为对象后,我需要使用rigth值修改一些属性,但不是全部。我需要将对象(已解码)发送回远程服务器。
答案 0 :(得分:0)
lodash.get
在这里很有用:
鉴于此对象:
{ a: { b: { c: { d: { e: { f: { g: {h : 1}}}}}}}}
_.get(obj, 'a.b.c.d.e.f.g.h')
返回1
如果未定义其中一个对象,则操作将返回undefined
而不是抛出。
答案 1 :(得分:0)
我终于找到了一个解决方案,通过推广一个属性解决方案而不添加外部库。它提供了一个返回一个新对象的递归函数:
exploreObject : function (source, target, value, n) {
if(n > 10000) // just to avoid infinite loop
return;
for(var i in source) {
//for every property in the source
if(source[i].hasOwnProperty('default')){
// if the property contains default values, set the same property in the target object to the default values
target[i] = source[i]['default'];
if (i == 'videoCapability' || i == 'ciscoCamera'){
//in my case, I had properties that needed to be changed from default to the 'value' value
target[i] = value;
}
} else if (typeof source[i] == 'object' && source[i] != null ){
//if the property doesn't contains default values and if it is an object, then we have to inspect this object
if (i == 'properties' && !NbPair(n)) {
//With JSON object, it can contains artefact property (with impair values of 'deepness' n), because coding to JSON can add a property 'properties' to store the properties. We don't need it, the values will be added to target object and not to target.properties
target = this.exploreObject(source[i], target, value, n + 1);
} else {
//target[i] has to be an object to store the new properties
target[i] = {};
target[i] = this.exploreObject(source[i], target[i], value, n + 1);
}
if (typeof target[i] == 'object' && isEmpty(target[i]) == true){
//After the call to the function itself, we delete the newly added object if it is empty (did not contained any default property, or the default value was empty)
delete target[i];
}
}
}
return target; //return the object with correct values
function isEmpty(obj) {
//function used to check if the object is empty
for(var key in obj) {
if(obj.hasOwnProperty(key))
return false;
}
return true;
}
function NbPair(Nb){
//function used to check if the 'deepness' in the object is pair or impair
if(Nb/2 == Math.round(Nb/2))
return true;
else
return false;
}
},