1

I am getting the data like this

[
  { "id":1,
    "name":"john"
  },
  { "id":2,
    "name":"ram"
  }
] 

I want to change the above data to below

 "array":
[
  { "id":1,
    "name":"john"
  },
  { "id":2,
    "name":"ram"
  }
]
3
  • this question does not belong on the angular tag... this is about js and json and not a specific framework Commented Feb 8, 2022 at 19:56
  • I want to do this question using angular only how can we achieve the output like that in angular Commented Feb 9, 2022 at 8:29
  • angular is a framework on top of the JavaScript Language. This question belongs into JS syntax, it is not angular specific. My answer below would work for angular as well as anything else using JS Commented Feb 9, 2022 at 21:01

2 Answers 2

2

The way to format your object in JavaScript would be something like this:

{ 
  "array": [
    { "id":1, "name":"john" },
    { "id":2, "name":"ram" }
  ]
{
Sign up to request clarification or add additional context in comments.

1 Comment

Can u tell me how to do this in angular, typescript.
0

I did not get this statement I want to do this question using angular only how can we achieve the output like that in angular as it's all about structuring/wrapping the JSON data. Hence, I added the solution in angular.js snippet below.

Demo (Angular) :

function ArrayController($scope) {
    const resData = [{
    "id":1,
    "name":"john"
    }, {
    "id":2,
    "name":"ram"
  }]; 
    $scope.data = {
    array: resData
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.1/angular.min.js"></script>
<div ng-app ng-controller="ArrayController">
    <div>{{ data }}</div>
</div>

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.