Pragnesh Ghoda  シ
Pragnesh Ghoda シ

Reputation: 8337

Remove Key Object from Array - Angular 5

I want to know how can we create const object variable with array, in which when we add item it should not contain index object.

e.g.

const estimateData = {
  'customerId': customerId,
  'vehicleInformation': {},
  'datalines': []
};

Here, When I add items in datalines, it always comes with key-value pair like below.

  "datalines": [
    0 : {
      "lineNumber": 1,
      ...
    },
    1 : {
      "lineNumber": 2,
      ...
    }
  ]

Because of this my webservice returns exception with error like

error: "Bad Request"
exception: "com.alldata.estimator.exceptions.BadRequestException"
message: "unknown dataline type 0"
path: "/estimator/estimates/211204"

So my question is how can I remove that 0,1 indexes so my request can be passed successfully and data can be approved. I have tried to add data via push, reassigning array, forEach loop but nothing worked.

  // I am trying to add data in array by following methods, but nothing works. 
  this.selectedEstimate.datalines.forEach((lineItem: ILineItem) => {
    estimateData.datalines.push(lineItem);
  });
  // estimateData.datalines = this.selectedEstimate.datalines;

I have also tried to parse data from JSON, but not worked.

Please let me know where is the issue and how can I solve this. Thanks in Advance.

Upvotes: 0

Views: 848

Answers (1)

Ankit Sharma
Ankit Sharma

Reputation: 1704

Stringify your estimateData before sending to the server.

Use the below code:-

JSON.stringify(estimateData)

const a = {
  'customerId': 1,
  'vehicleInformation': {},
  'datalines': []
};
a.datalines.push({age: 1});
a.datalines.push({age: 2});
a.datalines.push({age: 3});
a.datalines.push({age: 4});

// open browser console to check response
console.log(a);
console.log(JSON.stringify(a));

Please open the browser console to look at the indexes getting printed.

Upvotes: 1

Related Questions