Замените [объект объекта] на строку в угловых 2 скачать CSV - PullRequest
0 голосов
/ 26 июня 2018

my csv

У меня возникли проблемы с загрузкой данных в csv, я хочу, чтобы [объект объекта] изменился на строку в файле csv. но я попробую код, результат не определен.

это мой формат JSON, я беру только один пример данных

[
  {
    "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
        }
      ]
    }
  },
]

Это мой код в 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)
  });
}

как же изменить [объект Object] на строку?

Ответы [ 2 ]

0 голосов
/ 26 июня 2018

Вам нужно преобразовать ваших «инспекторов» в строку. Поэтому используйте JSON.stringify (object) следующим образом:

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

Только для имени инспектора:

    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 голосов
/ 26 июня 2018

Используйте JSON.stringify(object), это превратит ваш объект в читаемый человеком JSON.Не совсем уверен, что это то, что вы хотите.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...