使用Javascript:如何动态创建通过使用一个数组给定的对象名嵌套对象 [英] Javascript: how to dynamically create nested objects using object names given by an array

查看:122
本文介绍了使用Javascript:如何动态创建通过使用一个数组给定的对象名嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人能帮助我的JavaScript。

我有一个对象名为设置,我想编写一个函数,增加了新的设置到该对象。

新设置的名称和值是作为字符串。给予该设置的名称的串然后由下划线分成数组。新的设置应通过创建与由阵列的每个部分给出的名称的新嵌套对象,除了这应该是给予该设置的值的字符串的最后部分被添加到现有的设置对象。然后我应该能够提及的设置,并且例如提醒它的价值。我能做到这一点在这样一个静态的方式...

  VAR设置= {};
VAR newSettingName =Modules_Video_Plugin;
VAR newSettingValue =JWPlayer;
VAR newSettingNameArray = newSettingName.split(_);设置[newSettingNameArray [0] = {};
设置[newSettingNameArray [0] [newSettingNameArray [1] = {};
设置[newSettingNameArray [0] [newSettingNameArray [1]] [newSettingNameArray [2] = newSettingValue;警报(Settings.Modules.Mediaplayers.Video.Plugin);

...创建嵌套对象的部分是这样做的......

 设置[模块] = {};
设置[模块] [视频] = {};
设置[模块] [视频] [插件] =JWPlayer;

然而,由于构成部件数量的设置名称可以变化,例如一个newSettingName可能是Modules_Floorplan_Image_Src,我想这样做动态使用功能,如...

  createSetting(newSettingNameArray,newSettingValue);功能createSetting(设置值){
    // code创建新的设置放在这里
}

谁能帮我解决如何动态地做到这一点?

我presume必须有一个为...循环在那里通过数组itterate,但我一直没能制定出一个方法来创建嵌套的对象。

如果您百忙之中阅读,即使你不能帮助的时候走到这一步,非常感谢。


解决方案

 分配功能(OBJ,的keyPath,价值){
   lastKeyIndex = keyPath.length-1;
   对于(VAR I = 0; I< lastKeyIndex ++我){
     键=的keyPath [I]
     如果(!(OBJ中键))
       OBJ [关键] = {}
     OBJ = OBJ [关键]
   }
   OBJ [的keyPath [lastKeyIndex] =价值;
}

I hope someone can help me with this Javascript.

I have an Object called "Settings" and I would like to write a function that adds new settings to that object.

The new setting's name and value are provided as strings. The string giving the setting's name is then split by the underscores into an array. The new setting should get added to the existing "Settings" object by creating new nested objects with the names given by each part of the array, except the last part which should be a string giving the setting's value. I should then be able to refer to the setting and e.g. alert its value. I can do this in a static way like this...

var Settings = {};
var newSettingName = "Modules_Video_Plugin";
var newSettingValue = "JWPlayer";
var newSettingNameArray = newSettingName.split("_");

Settings[newSettingNameArray[0]] = {};
Settings[newSettingNameArray[0]][newSettingNameArray[1]] = {};
Settings[newSettingNameArray[0]][newSettingNameArray[1]][newSettingNameArray[2]] = newSettingValue;

alert(Settings.Modules.Mediaplayers.Video.Plugin);

... the part that creates the nested objects is doing this ...

Settings["Modules"] = {};
Settings["Modules"]["Video"] = {};
Settings["Modules"]["Video"]["Plugin"] = "JWPlayer";

However, as the number of parts that make up the setting name can vary, e.g. a newSettingName could be "Modules_Floorplan_Image_Src", I'd like to do this dynamically using a function such as...

createSetting (newSettingNameArray, newSettingValue);

function createSetting(setting, value) {
    // code to create new setting goes here
}

Can anyone help me work out how to do this dynamically?

I presume there has to be a for...loop in there to itterate through the array, but I haven't been able to work out a way to create the nested objects.

If you've got this far thanks very much for taking the time to read even if you can't help.

解决方案

function assign(obj, keyPath, value) {
   lastKeyIndex = keyPath.length-1;
   for (var i = 0; i < lastKeyIndex; ++ i) {
     key = keyPath[i];
     if (!(key in obj))
       obj[key] = {}
     obj = obj[key];
   }
   obj[keyPath[lastKeyIndex]] = value;
}

这篇关于使用Javascript:如何动态创建通过使用一个数组给定的对象名嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆