0

I have a Grocery List that I am able to create from an array of foods. What I am trying to do is name the array when I store it. I have my copy hung here Plunker

Currently the output looks like

    [
  {
    "id": 3,
    "name": "Coconuts"
  },
  {
    "id": 2,
    "name": "Peaches"
  },
  {
    "id": 1,
    "name": "Oranges"
  }
]

I would like it to be something like

    [
  {"John's List":
  {
    "id": 3,
    "name": "Coconuts"
  },
  {
    "id": 2,
    "name": "Peaches"
  },
  {
    "id": 1,
    "name": "Oranges"
  }}
]

any thoughts or suggestions would be greatly appreciated.

2
  • That's not valid JSON. Do you mean the items within "John's List" to be an array also? Commented Sep 28, 2014 at 6:00
  • It is possible. but you have to mention it properly. when should it happen? what should be the flow? Your plunker doesn't tell anything. Moreover, it seems incomplete too. Commented Sep 28, 2014 at 6:42

1 Answer 1

1

updated your save function like below

            $scope.save = function () {
              var entity = {};
              entity[$scope.name] = $scope.NewList;
                $scope.MyList = angular.copy(entity);
            };

from

           $scope.save = function () {
                $scope.MyList = angular.copy($scope.NewList);
            };

checkout this update plunker

the JSON output is

 {
  "john's list": [
    {
      "id": 3,
      "name": "Coconuts",
      "Amount": 10,
      "Price": 10
    },
    {
      "id": 2,
      "name": "Peaches",
      "Amount": 5,
      "Price": 5
    }
  ]
}

does this matches your expected output??

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

1 Comment

Thats exactly what I was looking for sorry my Json wasnt accurate. Thank you for the help.

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.