2

I have a JavaScript function that makes an ajax call to an API, and gets a JSON array.

Here is a sample of an array that I get:

[
  {
    "ErrorType": "Errors",
    "Explanations": [
      {
        "Explanation": "Price Missing",
        "Locations": [
          25,
          45
        ]
      },
      {
        "Explanation": "Unit of measurement not valid",
        "Locations": [
          25,
          301,
          302
        ]
      }
    ]
  },
  {
    "ErrorType": "Warnings",
    "Explanations": [
      {
        Blablabla,
        Ithinkthere's already much here
      }
    ]
  }
]

I put it into a JavaScript array:

$scope.CorrectionDatas = ResponseFromApi;

So, for each ErrorType, I have some "explanations". I would like to add another property, in order to have something like this :

[
  {
    "ErrorType": "Errors",
    "Explanations": [
      {
        "Explanation": "Price Missing",
        "Locations": [
          25,
          45
        ]
      },
      {
        "Explanation": "Unit of measurement not valid",
        "Locations": [
          25,
          301,
          302
        ]
      }
    ],
    "show": true
  },
  {
    "ErrorType": "Warnings",
    "Explanations": [
      {
        Blablabla,
        Ithinkthere's already much here 
      }
     ],
    "show":true
  }
]

I though that I could do it only by doing it like that:

$scope.CorrectionDatas.forEach(function (error) {
    error.push({ show: true });
});

But my debugger gives me an error:

Error: error.push is not a function 
$scope.getErrors/</<@http://localhost:1771/dependencies/local/js/Correction/CorrectionCtrl.js:26
3
  • 1
    The problem is that while the JSON response constitutes an array, each element of that array is an object, not an array. I.e {"ErrorType":"Warnings", "Explanations":[{ Blablabla, I think there's already much here }]} is an object... Commented Aug 28, 2013 at 12:51
  • 1
    Can you please show the output of 'typeof error'? I believe this to be an object, not an array. Commented Aug 28, 2013 at 12:52
  • as it is said, I received an object, thx for your help Commented Aug 28, 2013 at 12:56

3 Answers 3

4

Each error is an object so it don't have push, the code should be:

        $scope.CorrectionDatas.forEach(function(error) {
            error.show = true;
        });
Sign up to request clarification or add additional context in comments.

Comments

3

Try this way:

$scope.CorrectionDatas.forEach(function (error){
     error["show"] = true;
});

Comments

3

I believe that the problem you're encountering is that error is not an array, it is an object. This can be confirmed by logging the output of typeof error. If this is the case, you must explicitly define the show property of the object, as so:

$scope.CorrectionDatas.forEach(function (error){
    error['show'] = true; // alternatively, error.show = true;   
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.