Change [object Object] to string in angular 2 download csv

1.7k views Asked by At

my csv

I got some problems in my data download csv, i want [object Object] change to string in file csv. but i have try code the result is undefined.

this is my JSON format, i only take one example data

[
  {
    "id": 117,
    "code": "RR123",
    "dueDate": "2018-06-25T09:51:00",
    "isActive": true,
    "inspectors": {
      "id": 67,
      "employeeNumber": "18001",
      "name": "Larks Anderson",
      "isActive": true
    },
    "questioners": {
      "id": 63,
      "code": "PI190",
      "name": "Inspeksi Door",
      "isActive": true,
      "questionerDetails": [
      {
          "id": 124,
          "questionDetail": "",
          "isActive": true
        }
      ]
    }
  },
]

This is my code in component.ts

//Button CSV
getcsvFile() {
  this.workorderService.getAllWorkOrder().subscribe(data => {
    this.allpagingData = [];
    let questionerlabel: any;

    for (let index in data) {
      console.log(data[index].questioners);

     //i use this to change the [object Object], but the result is undefined in csv
      for (let ai in data[index].questioners) {
        questionerlabel = data[index].questioners[ai].name;
        console.log(questionerlabel);
      }

      this.allpagingData.push({
        "code": data[index].code,
        "inspectors": data[index].inspectors,
        "questioners": questionerlabel,
        "dueDate": data[index].dueDate,
        "isActive": data[index].isActive
      });
    }
    var option = {
      fieldSeparator: ',',
      quoteStrings: '"',
      decimalseparator: '.',
      showLabels: true,
      showTitle: true,
      headers: ['WO Code' , 'Inspectors', 'Questioner', 'Date', 'Status']
    }

    new Angular2Csv(this.allpagingData, 'WorkOrder Report', option)
  });
}

so how to change [object Object] to string?

3

There are 3 answers

10
Prachi On BEST ANSWER

You need to convert your 'inspectors' into a string. So use JSON.stringify(object) like this:

    this.allpagingData.push({
    "code": data[index].code,
    "inspectors": data[index].inspectors ? JSON.stringify(data[index].inspectors) : '',
    "questioners": questionerlabel ? questionerlabel : 'N/A',
    "dueDate": data[index].dueDate,
    "isActive": data[index].isActive
     });

For just inspector's name:

    this.allpagingData.push({
    "code": data[index].code,
    "inspectors": data[index].inspectors ? data[index].inspectors.name : '',
    "questioners": questionerlabel ? questionerlabel : 'N/A',
    "dueDate": data[index].dueDate,
    "isActive": data[index].isActive
     });
0
Thor Jacobsen On

Use JSON.stringify(object), that'll turn your object into human-readable JSON. Not really sure that's what you want, though.

0
ResolveError On

Below is best answer for resolving undefined and [object object] error


          yourdata.forEach((x, len) => {
            if (x.length === undefined) {
              x.values = JSON.stringify(x.values);
            }
            else {

              let nfvdata = "";
              let nfvobject = {};
              x.forEach(y => {
                if (typeof (y) === "string") {
                  nfvobject["questioners"] = y;

                }
                else if (y["questioners"] == undefined) {
                  nfvdata = nfvdata + JSON.stringify(y)
                  nfvobject["questionerDetails"] = nfvdata;
                }

                else {
                  //yourdata.push(y);
                }

              });
               yourdata.splice(len, 1, nfvobject);
              // yourdata.push(nfvobject);
            }
          });
          let record = yourdata[1] == undefined ? dataSet["data"] : dataSet["data"][1];
          this.csvOptions.headers = Object.keys(record);
          new AngularCsv(yourdata, this.Filename, this.csvOptions);

this another approach to process JSON as like your's

 yourdata.forEach((x, len) => {
            if (x.length === undefined) {
              x.values = JSON.stringify(x.values);
            }
            else {
              yourdata.splice(len, 1);
              x.forEach(y => {
                yourdata.push(y);
              })
            }
          });

          let record = yourdata[1] == undefined ? yourdata : yourdata[1];
          this.csvOptions.headers = Object.keys(record);
          new AngularCsv(dataSet["data"], this.Filename, this.csvOptions);
        }