在AngularJS中使用ngCSV之前处理数组 [英] Manipulating array before using ngCSV in AngularJS

查看:64
本文介绍了在AngularJS中使用ngCSV之前处理数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要转换为CSV文件的数组。看起来像这样:

I have an array that I want to turn into a CSV file. Looks like this:

filteredRecords [
{
    date: '', 
    time: '', 
    comments: [
        {message: '',
        commenttime: ''},
        {message: '',
        commenttime: ''},
        {message: '',
        commenttime: ''}
    ],
    arrival: '',
},
{
    date: '', 
    time: '', 
    comments: [
        {message: '',
        commenttime: ''},
        {message: '',
        commenttime: ''},
        {message: '',
        commenttime: ''}
    ],
    arrival: '',
}
]

ngCSV对于普通数组来说就像一个咒语,但是它不能处理嵌套数组

ngCSV works like a charm for normal arrays, but it can't handle nested arrays.

我想将注释中的嵌套数组转换为字符串,因此我可以使用ngCSV导出文件,然后执行相反的操作

I want to turn the nested array in comments into a string, so I can use ngCSV to export the file, and do the opposite when importing the file.

它应该看起来像这样:

filteredRecords [
{
    date: '', 
    time: '', 
    comments: '[{"message":"test","commenttime":"16.29"},{"message":"test","commenttime":"16.29"}]',
    arrival: '',
}

我尝试了以下操作:

$scope.commenttoJson = function(filteredRecords){
        $scope.json = angular.toJson(filteredRecords);
        console.log($scope.json);
}

但这会将整个 filteredRecords 数组变成一个Json对象。

But this turns the whole filteredRecords array into a Json object.

我如何设法仅操作注释对象?

How can I manage to manipulate only the comments object?

推荐答案

这样的事情对您有用吗?

Something like this work for you?

var filteredRecords = [
{
    date: '', 
    time: '', 
    comments: [
        {message: '',
        commenttime: ''},
        {message: '',
        commenttime: ''},
        {message: '',
        commenttime: ''}
    ],
    arrival: '',
},
{
    date: '', 
    time: '', 
    comments: [
        {message: '',
        commenttime: ''},
        {message: '',
        commenttime: ''},
        {message: '',
        commenttime: ''}
    ],
    arrival: '',
}
];

var newArr = [];

for (var i = 0; i < filteredRecords.length; i++) {
  newArr.push(filteredRecords[i]);
  if (newArr[newArr.length - 1].comments) {
    newArr[newArr.length - 1].comments = JSON.stringify(newArr[newArr.length - 1].comments);
  }
}

alert(JSON.stringify(newArr));

这篇关于在AngularJS中使用ngCSV之前处理数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆