Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
137 views
in Technique[技术] by (71.8m points)

javascript - Convertir .xml为JSON(Convertir .xml a JSON)

I'm loading an Excel file and with the SheetJS library I convert it into a JSON, but I receive it this way:(我正在加载一个Excel文件,并通过SheetJS库将其转换为JSON,但是我通过以下方式收到它:)

JSON.parse(output)(JSON.parse(输出))

data: Array(15)
0: (11) ["id", "created", "device", "plataforma", "locale", "first_render_time", "total_load_time", "total_size", "load_without_js", "request", "score"]
1: (11) ["424", "2019-11-19T23:58:07.977Z", "mobile", "PSI", "es-CO", "9840", "37602", "6286909", "3978", "227", "0"]
2: (11) ["423", "2019-11-19T23:57:43.340Z", "desktop", "PSI", "es-CO", "1981", "7903", "5424580", "3452", "204", "0.19"]
3: (11) ["422", "2019-11-19T23:55:24.163Z", "mobile", "PSI", "es-CO", "10052", "38450", "5850544", "3506", "231"

The first row must be the JSON name .(第一行必须是JSON name 。)

I want to receive it this way.(我想这样收到。)

[
  {
    id: 6049,
    created: "2019-11-29T20:05:22.870Z",
    device: "desktop",
    plataforma: "PSI",
    locale: "es-CO",
    first_render_time: 2042,
    total_load_time: 7904,
    total_size: 6000627,
    load_without_js: 3722,
    request: 212,
    score: "0.18"
  },
  {
    id: 6046,
    created: "2019-11-29T20:00:23.317Z",
    device: "desktop",
    plataforma: "PSI",
    locale: "es-CO",
    first_render_time: 2052,
    total_load_time: 7974,
    total_size: 6001442,
    load_without_js: 3648,
    request: 212,
    score: "0.18"
  }
]
  ask by Rafael Pereira translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Split the header (1st row) from the body (all other rows) by destructuring the array.(通过拆分从所述主体(所有其他行)的报头(第1行) 解构阵列。)

Iterate the body using Array.map() , and then reduce the header to an object, and take the data from the current array:(使用Array.map()迭代主体,然后将标头简化为一个对象,并从当前数组中获取数据:)

 const convertToObjects = ([header, ...body]) => body.map(arr => header.reduce((r, prop, i) => { r[prop] = arr[i]; return r; }, {})); const data = [ ["id", "created", "device", "plataforma", "locale", "first_render_time", "total_load_time", "total_size", "load_without_js", "request", "score"], ["424", "2019-11-19T23:58:07.977Z", "mobile", "PSI", "es-CO", "9840", "37602", "6286909", "3978", "227", "0"], ["423", "2019-11-19T23:57:43.340Z", "desktop", "PSI", "es-CO", "1981", "7903", "5424580", "3452", "204", "0.19"] ]; const result = convertToObjects(data); console.log(result); 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...