如何在javascript中将对象数组转换为简单数组?

时间:2022-03-29 07:06:18

I try to use an AJAX request with datatable, but my JSON has the wrong format. So I try to convert the structure. I have a JSON string as an array of objects like this form:

我尝试使用带数据表的AJAX请求,但我的JSON格式错误。所以我尝试转换结构。我有一个JSON字符串作为这种形式的对象数组:

[  
    {  
          "timestamp":"1461308319473",
          "id":"1",
          "value":"77"
    },
    {  
          "timestamp":"1461308458181",
          "id":"2",
          "value":"99"
    }
] 

But I need this structure:

但是我需要这个结构:

{
  "data": [
    [
      "1461308319473",
      "1",
      "77"
    ],
    [
      "1461308458181",
      "2",
      "99"
    ]
  ]
}

How can I convert the arrays? I would like to use datatable with this command:

我怎样才能转换数组?我想在这个命令中使用datatable:

$("#table")
    .dataTable({
        "ajax": {
            "type": "GET",
            "url": "https://url",
            "dataSrc": function (jsonData) {
            for (var i = 0; i < jsonData.length; i++) {
                returnData.push({
                    'timestamp': moment.unix(jsonData[i].timestamp / 1000).format("DD.MM.YY hh:mm:ss"),
                    'id': jsonData[i].id,
                    'value': jsonData[i].value
                });
            }
            return returnData;
        },
        "columns": [
            { "data": "timestamp" },
            { "data": "id" },
            { "data": "value" }
        ]
    }
});

At moment I get the following error: 如何在javascript中将对象数组转换为简单数组?

目前我收到以下错误:

Thanks

谢谢

3 个解决方案

#1


1  

you can do something like:

你可以这样做:

var keys = ['timestamp', 'id', 'value'],
    data = jsonData.map(function(datum){
      return keys.map(function(key){return datum[key]})      
    })

data  = {data:data}
console.log(data)

in ES6:

在ES6中:

let keys = ['timestamp', 'id', 'value'],
  data = jsonData.map( datum => keys.map(key => datum[key]))

data = {data}

Fiddle Demo

小提琴演示

#2


1  

If you are sue that there are only timestamp, id and value then

如果你是sue,那么只有时间戳,id和值

var data = [ 
    {  
          "timestamp":"1461308319473",
          "id":"1",
          "value":"77"
    },
    {  
          "timestamp":"1461308458181",
          "id":"2",
          "value":"99"
    }
] ;

var result = {};
result.data = data.map(function(d) {
    return [
        d.timestamp,
        d.id,
        d.value
    ];
});

document.write(['<pre>', JSON.stringify(result, 0, 3), '</pre>'].join(''));

#3


0  

You can use the following example to create an object that has a data property which is an empty array and then use the forEach() method on your array to populate as such:

您可以使用以下示例创建一个具有data属性的对象,该对象是一个空数组,然后使用数组上的forEach()方法填充:

var objectWithArray = {'data': []};    
var arrWithObject = [  
        {  
              "timestamp":"1461308319473",
              "id":"1",
              "value":"77"
        },
        {  
              "timestamp":"1461308458181",
              "id":"2",
              "value":"99"
        }
    ] 
    arrWithObject.forEach(functio(v,i){
        objectWithArray.data.push([v.timespam, v.id, v.value])
    })

#1


1  

you can do something like:

你可以这样做:

var keys = ['timestamp', 'id', 'value'],
    data = jsonData.map(function(datum){
      return keys.map(function(key){return datum[key]})      
    })

data  = {data:data}
console.log(data)

in ES6:

在ES6中:

let keys = ['timestamp', 'id', 'value'],
  data = jsonData.map( datum => keys.map(key => datum[key]))

data = {data}

Fiddle Demo

小提琴演示

#2


1  

If you are sue that there are only timestamp, id and value then

如果你是sue,那么只有时间戳,id和值

var data = [ 
    {  
          "timestamp":"1461308319473",
          "id":"1",
          "value":"77"
    },
    {  
          "timestamp":"1461308458181",
          "id":"2",
          "value":"99"
    }
] ;

var result = {};
result.data = data.map(function(d) {
    return [
        d.timestamp,
        d.id,
        d.value
    ];
});

document.write(['<pre>', JSON.stringify(result, 0, 3), '</pre>'].join(''));

#3


0  

You can use the following example to create an object that has a data property which is an empty array and then use the forEach() method on your array to populate as such:

您可以使用以下示例创建一个具有data属性的对象,该对象是一个空数组,然后使用数组上的forEach()方法填充:

var objectWithArray = {'data': []};    
var arrWithObject = [  
        {  
              "timestamp":"1461308319473",
              "id":"1",
              "value":"77"
        },
        {  
              "timestamp":"1461308458181",
              "id":"2",
              "value":"99"
        }
    ] 
    arrWithObject.forEach(functio(v,i){
        objectWithArray.data.push([v.timespam, v.id, v.value])
    })