2

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
  • What parameters are accepted by Angular2Csv function? Commented Jun 26, 2018 at 7:19
  • Angular2Csv is module from angular, i used module and import { Angular2Csv } from 'angular2-csv/Angular2-csv'; on the top my component.ts Commented Jun 26, 2018 at 7:25
  • the parameters are accepted is (any,string,any) Commented Jun 26, 2018 at 7:26

3 Answers 3

2

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
     });
Sign up to request clarification or add additional context in comments.

10 Comments

ok thank you, it's worked. but how if i want get only detail the name in object inspectors?
just inspector's name?
I have updated the answer. For questionerlabel also you can use condition to avoid undefined in your csv.
hello prachi, in case just inspector name,its still error code, cannot inspectors.name. the sytanx name was error code thx
The example which you have shown in the question represents it as an object, but in the code, it is showing inspectors as an array. So, inspectors.name will only work in the case of an object type, not an array type. Did your inspector type change?
|
2

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

Comments

0

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);
        }

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.