如何将多维数组转换为json对象(节点js)

时间:2018-08-07 15:13:35

标签: arrays json node.js

我正在从API中获取几个值,并将它们存储为数组。我正在尝试将此数组转换为JSON对象。这是我当前得到的输出:

[ { Service: '1e',
    Maintainability: 'A',
    Issues: 2,
    Coverage: 1.2 } ]  
[ { Service: '2',
    Maintainability: 'B',
    Issues: 2,
    Coverage: 0.6 } ] 
[ { Service: '3',
    Maintainability: 'C',
    Issues: 5,
    Coverage: 0.2 } ] 
[ { Service: '4',
    Maintainability: 'A',
    Issues: 1,
    Coverage: 1 } ]

我需要输出看起来像这样:

[ { Service: '1e',
    Maintainability: 'A',
    Issues: 2,
    Coverage: 1.2 },  
  { Service: '2',
    Maintainability: 'B',
    Issues: 2,
    Coverage: 0.6 }, 
  { Service: '3',
    Maintainability: 'C',
    Issues: 5,
    Coverage: 0.2 }, 
  { Service: '4',
    Maintainability: 'A',
    Issues: 1,
    Coverage: 1 } ]

请帮忙吗?

2 个答案:

答案 0 :(得分:1)

这是一个示例:

let arr1=[{a:1}];
let arr2=[{b:2}];
let arr3=[{c:3}];
let Result = arr1.concat(arr2,arr3); // Result = [{a:1},{b:2},{c:3}]

let arr4=[
    [{a:1}],
    [{b:2}],
    [{c:3}],
];
let Result2 = [].concat(...arr4); //Result2 = [{a:1},{b:2},{c:3}]

同样,地图也是个好主意

答案 1 :(得分:0)

如果您得到此提示:

def store_results (time, code):
# implement this however you want

所需的数组是:

porthit(servername, port)

或者您可以这样做:

2018-08-07T15:55:46.113097: No connection adapters were found for 'httpz://www.example.dev:80/'
2018-08-07T14:55:48: server code 200
2018-08-07T15:55:58.502277: server code 404
2018-08-07T15:56:21.509191: server code 200