javascript - Converting js array into dictionary map - Stack Overflow

I have this array:["userconfig", "general", "name"]and I would like it to

I have this array:

["userconfig", "general", "name"]

and I would like it to look like this

data_structure["userconfig"]["general"]["name"]

I have tried this function:

inputID = "userconfig-general-name"

function GetDataByID(inputID){

    var position = '';

    for (var i = 0; i < inputID.length; i++) {
        var hirarchy = inputID[i].split('-');

        for (var index = 0; index < hirarchy.length; index++) {
            position += '["'+ hirarchy[index] +'"]';
        }
    }
    return data_structure[position];
}

while hirarchy is the array. I get the [position] as a string which is not working well.

how can I make a js function which builds the object path dynamically by an array?

I have this array:

["userconfig", "general", "name"]

and I would like it to look like this

data_structure["userconfig"]["general"]["name"]

I have tried this function:

inputID = "userconfig-general-name"

function GetDataByID(inputID){

    var position = '';

    for (var i = 0; i < inputID.length; i++) {
        var hirarchy = inputID[i].split('-');

        for (var index = 0; index < hirarchy.length; index++) {
            position += '["'+ hirarchy[index] +'"]';
        }
    }
    return data_structure[position];
}

while hirarchy is the array. I get the [position] as a string which is not working well.

how can I make a js function which builds the object path dynamically by an array?

Share Improve this question edited Jul 22, 2014 at 7:17 itay101 asked Jul 22, 2014 at 7:11 itay101itay101 531 silver badge5 bronze badges 4
  • Did you try anything? – KooiInc Commented Jul 22, 2014 at 7:13
  • what would be a value for such a structure? – Jan Pfeifer Commented Jul 22, 2014 at 7:17
  • I have edited my question, please review it again – itay101 Commented Jul 22, 2014 at 7:18
  • Why you split inputID[i] by '-'? – hindmost Commented Jul 22, 2014 at 7:21
Add a ment  | 

3 Answers 3

Reset to default 2
var arr = ["userconfig", "general", "name"];
var dataStructure = arr.reduceRight(function (value, key) {
    var obj = {};
    obj[key] = value;
    return obj;
}, 'myVal');

Ends up as:

{ userconfig : { general : { name : 'myVal' } } }

Note that you may need a polyfill for the reduceRight method: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/ReduceRight

The below function will take an object to modify and an array filled with the properties needed:

function objPath(obj,path){
   path.forEach(function(item){
      obj[item] = {};
      obj = obj[item];
   });
}

var myobj = {};
objPath(myobj,["test","test2","test3"]);

console.log(myobj);
//outputs
Object {test: Object}
    test: Object
        test2: Object
            test3: Object

The function loops over the array creating the new object property as a new object. It then puts a reference to the new object into obj so that the next property on the new object can be made.

JSFiddle

Recursive function

var array = ["userconfig", "general", "name"];

function toAssociative(array) {
    var index = array.shift();
    var next = null;
    if (array.length > 0) {
        next = toAssociative(array);
    }

    var result = new Array();
    result[index] = next;

    return result;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745285970a4620562.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信