2

I have an object created from JSON via AJAX from the server. The object has several sub-objects in an array, e.g.:

obj.subObj1[0].value="abc";
obj.subObj1[1].value="abc";
obj.subObj2[0].value="abc";

Now I want to set some values in this object but I dont know if they already exist.

obj.subObj1[0].value="new Value";  // No Problem
obj.subObj2[1].value="new Value2"; // Problem because obj.subObj2[1] is no Object.

I would need to do obj.subObj2[1]={} first.

Because I have this problem very often I am looking for method to automate this. A method or class which does automatically create the needed object (or array if I use an integer).

It should be able to handle an infinite depth of such sub-objects. Like this:

var obj = TheObject();
obj.sub1.sub2[10].sub3[1].sub4='value';

Now automatically all needed sub-objects and arrays should be created.

3 Answers 3

1

Cannot really guarantee anything about cross-browser compatibility, but how about trying this on for size (works in Chrome):

// Safely sets value property of index of an array of an object.
function setObj(obj, subObjName, index, val) {
    // Ensure the object exists
    if (typeof obj == 'undefined') {
        obj = new Object();
    }

    // Ensure the array property exists
    if (typeof obj[subObjName] == 'undefined') {
        obj[subObjName] = new Array();
    }

    // Ensure the array properties index exists
    if (typeof obj[subObjName][index] == 'undefined') {
        obj[subObjName][index] = {};
    }

    // Set the value
    obj[subObjName][index].value = val;

    // Return the object
    return obj;
}

Example use:

<script type="text/javascript">
    var obj;
    obj = setObj(obj, "something", 1, "val");
    setObj(obj, "something", 0, "someValue");
    alert(obj.something[1].value);
    alert(obj.something[0].value);
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

This works for the limit example. I am looking for a generic solution for unlimit depth in the objects. I also want to do obj.some[0].other[1].another.value and so on.
That's not really what you asked, but you can change your question a little to reflect that. In order to do something like that, I would imagine you would need a string parsing function to automagically create the appropriate object and array variables.. the resulting call would look something like this: parentObject = createBlueSmoke("obj.some[0].other[1].another.value", "some value");
0

If you can assume that the referenced item in the array will be either undefined or an object it simplifies things. Of course the simple (non-automatic) way would be something like this:

if (!obj.subObj2[1]) obj.subObj2[1] = {};
obj.subObj2[1].value = "new Value2";

A not-very generic function to do it for you would be:

function setArrayObjectProp(arr, index, prop, val) {
   if (!arr[index])
       arr[index] = {};

   arr[index][prop] = val;
}

// called as
setArrayObjectProp(obj.subObj2, 1, "value", "new Value2");

Comments

0

heloo

try testing the type of the array item first if its not object then equal it to the new object format {value:"new Value2"}

if(typeof(obj.subObj2[1])!='object')
{
obj.subObj2[1] = {value:"new Value2"};
}
else
{
obj.subObj2[1].value = "new Value2";
}

Comments

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.