-1

I need to create the object property from array values and assign some values to that for example

var main_obj = {};
var dynamic_array = ["value1", "value2", "value3", "value4"];

From dynamic_array I need to create main_obj like:

main_obj[value1][value2][value3] = some_value;

Please give me some suggestions.

5
  • 1
    Do you mean: main_obj[dynamic_array[0]] etc... ? Commented Oct 27, 2014 at 13:51
  • Well, if it so, you can use something weird like this code. Commented Oct 27, 2014 at 13:54
  • where are values coming from? This question is really lacking in detail Commented Oct 27, 2014 at 14:11
  • @charlietfl Am getting the values at run-time and push into the dynamic_array Commented Oct 28, 2014 at 5:00
  • actually my main_obj = {"value1":{"value2":{"value3":}}} is like this what i need to do is i need to parse through until value3 and add the new object, the dynamic_array is a reference for how long i need to parse. Commented Oct 28, 2014 at 6:02

4 Answers 4

0
dynamic_array.forEach(function(prop){
   main_obj[prop] = some_value;  
});
Sign up to request clarification or add additional context in comments.

Comments

0

this is another way

var dynamic_array = ["value1","value2","value3"];
var value = 'your value';

function createobj(dynamic_array,value){
    var count = dynamic_array.length;
    var text= '';
    var ended = '';
    for(var i = 0; i < count; i++){
      text += '{"'+dynamic_array[i]+'":';
      ended +='}';
    }
    text += '"'+value+'"'+ended;
    return JSON.parse(text);
}
console.log(createobj(dynamic_array,value));

2 Comments

actually my main_obj = {"value1":{"value2":{"value3":}}} is like this what i need to do is i need to parse through until value3 and add the new object, the dynamic_array is a reference for how long i need to parse. thanks
actually my main_obj = {"value1":{"value2":{"value3":}}} is like this what i need to do is i need to parse through until value3 and add the new object, the dynamic_array is a reference for how long i need to parse.
0

Trying to interpretate your question, this may be a solution to what you are looking for:

function createNestedProperties(obj, array, value) {
    var result = {};
    var result_tmp = obj;
    var i = 0;
    while (i < array.length) {
        if (i < (array.length - 1))
            result_tmp[array[i]] = {};
        else
            result_tmp[array[i]] = value;
        result_tmp = result_tmp[array[i]];
        i++;
    }
    return obj;
}

used this way:

var main_obj = {"value1":{"value2":{"value3":{}}}};
var dynamic_array = ["value1", "value2", "value3"];

main_obj = createNestedProperties(main_obj, dynamic_array, {"value_Z":"new value"});

Resulting object:

{"value1":{"value2":{"value3":{"value_Z":"new value"}}}}

This function would override existing properties in main_obj if already there.

1 Comment

@Troy I have slightly updated the above code, using that function you will be able to set a new {...} object to "value3" property (possibly overriding an existing value if present).
0

i parse through the object recursively rather than building that jQuery - How to recursively loop over an object's nested properties?

    function recursiveIteration(main_obj) {
        for (var property in main_obj) {
            if (property == value3)
            {
                main_obj[property] = {"value4":test};
            }
            else
            {
                recursiveIteration(main_obj[property]);
            }
        }
    }
    recursiveIteration(main_obj);

1 Comment

That's not an absolute solution, only works with fixed property, should not be preferred to other (more correct) suggested ones.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.