1

I have a json object as shown.

this.model = {
            "cellType": "RemoveCell",
            "maxWidth": "800px",
            "items": {
                "columns" : [
                    {"attribute": "existingStrings", "title": "Existing Strings"},
                ],
                "rows" : [
                    {"existingStrings": this.stringList},
                ]
            }
        },

this.stringList is an array of strings (of varying size). How do declare this.stringList in the JSON object so that it behaves in this manner below?

this.model = {
            "cellType": "RemoveCell",
            "maxWidth": "800px",
            "items": {
                "columns" : [
                    {"attribute": "existingStrings", "title": "Existing Strings"},
                ],
                "rows" : [
                    {"existingStrings": this.stringList[0]},
                    {"existingStrings": this.stringList[1]},
                    {"existingStrings": this.stringList[2]},
                                      ...
                                      ...
                    {"existingStrings": this.stringList[n]},
                ]
            }
        },

I want "rows" to contain an array of objects (one object for each element in stringList). Thanks

3
  • do it with a for loop Commented Aug 7, 2014 at 16:18
  • But I can't iterate in a json object. This is fine if the size of the array is constant and known. In my case it is not. I can't hardcode it like this. Thanks Commented Aug 7, 2014 at 16:19
  • do a foor loop on this.model.items.rows.existingStrings.length, and build the array of objects you need Commented Aug 7, 2014 at 16:20

2 Answers 2

4

Use a function to build the list of objects

this.model = {
        "cellType": "RemoveCell",
        "maxWidth": "800px",
        "items": {
            "columns" : [
                {"attribute": "existingStrings", "title": "Existing Strings"},
            ],
            "rows" : 
                (function(){ 
                  var objList = []; 
                  for(var i = 0; i < this.stringList.length; i++)
                   objList.push({"existingStrings":this.stringList[i]});
                  return objList;
                })()
        }
    },

This works by using an IIFE (Immediately invoked function expression) as the value for "rows". (function(){}) defines the function expression. When the () is used following the expression the function is invoked. Inside of the function is a for loop which builds an array of objects using this.stringList. Once the loop finishes, the array is returned, and that is the value which is assigned to "rows".

Sign up to request clarification or add additional context in comments.

4 Comments

May be useful to precise to the reader that you will then have to call your array using this.model.items.rows() instead of this.model.items.rows.
@dgiugg - That is incorrect. The function is an immediately invoked function expression and evaluates to the array of objects.
This is a more elegant answer than mine. You've left out the length attribute in the for loop though - this.stringList.length.
I added a slight explanation of the process below, and also included .length (thanks for catching that).
2
var rows = [];

for ( var i=0; i<this.stringList.length; i++){
    rows.push({"existingStrings":this.stringList[i]});
}

this.model = {
        "cellType": "RemoveCell",
        "maxWidth": "800px",
        "items": {
            "columns" : [
                {"attribute": "existingStrings", "title": "Existing Strings"},
            ],
            "rows" : rows
        }
    },

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.