0

Update: I'm trying to construct a table out of my data structure(ie section1) and then allow users to add rows to the table to insert more rows and save them to my datastructure.

I have an array newArr in the form of key value pairs. When somebody clicks a button, I want to be able to push the newArray into the Groups.I dont seem to be able to push into the Groups array. Chrome dev tools shows Groups as Objects and i'm not certain how to loop through and append to each item of the Groups Object. Feel free to modify the $scope.section1 to a different datastructure that could make it easier to push new items to it.

$scope.section1 = [{
                "id":1, "Name": "Section 1: Inventory",
                 "Groups":[
                     {"cell" : "Number", "cellValue" : "value1"}, 
                     {"cell" : "Location", "cellValue" : "value2"}, 
                     {"cell" : "Severity", "cellValue" : "value3"}
                 ],
                 "FootNotes":[
                    {"templateurl" : "components/section/templates/notes/section1.notes.html"}
                 ]  
            }]

        var newArr = {"cellValue" : "value4","cellValue" : "value5","cellValue" : "value6"}

So the output should look like

 $scope.section1 = [{
                    "id":1, "Name": "Section 1: Inventory",
                     "Groups":[
                         {"cell" : "Number", "cellValue" : "value1", "cellValue" : "value4"}, 
                         {"cell" : "Location", "cellValue" : "value2", "cellValue" : "value5"}, 
                         {"cell" : "Severity", "cellValue" : "value3", "cellValue" : "value6"}
                     ],
                     "FootNotes":[
                        {"templateurl" : "components/section/templates/notes/section1.notes.html"}
                     ]  
                }]
1
  • an object cant have two identical keys. Commented Feb 17, 2016 at 20:19

2 Answers 2

1

You can't have two properties with the same name. You have cellValue two times for Object group. What are you trying to do?

You'd better like to change the structure itself:

...
"Groups": [
    { 
        name: "group1",
        values: ['value 1', 'value 2', 'value 3']
    }
]

Then, adding one more value to group1:

$scope.section1[0].Groups.values.push('value 4')

Note than I'm trying to respect the whole structure that you already have, but I don't meaning this is the optimal way to solve your problem

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

Comments

0
$scope.section1[0].Groups.push({
  "cell" : "Number", 
  "cellValue" : "value1", 
  "anotherCellValue" : "value4"
});

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.