我正在将表数据作为字符串数组获取,如何将它们转换为jsons数组 [英] Am getting the table data as array of strings, how to convert them to array of jsons

查看:85
本文介绍了我正在将表数据作为字符串数组获取,如何将它们转换为jsons数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是正在获取数据

["node_name", "ip", "name", "active", "0YXe_ws", "10.0.10.147", "generic", "0", ]

前四个元素是列名,后四个元素是这些列名的值.我希望数据为

First four elements are column names and next four elements are values of those column names. I want the data as

[
  {
    "node_name": "0YXe_ws",
    "ip":"10.0.10.147", 
    "name":"generic", 
    "active":"0"
  }
]

我尝试将数组除以四个元素,但没有用

I have tried by dividing the array by four elements and it didnt work

推荐答案

如果可以确保所提到的strucutre始终相同,那么简单的for循环就可以解决问题.

If you can make sure your mentioned strucutre is always the same, a simple for loop should do the trick.

只需将数组长度除以遍历所有键,然后将数组的一半长度加到每个键上即可得到相应的值.

Simply divide the array length to run over all keys and add the half length of the array to each key to get the corresponding value.

let arr = ["node_name", "ip", "name", "active", "0YXe_ws", "10.0.10.147", "generic", "0"];
let data = {}
for(let i=0;i<arr.length / 2;i++){
  data[arr[i]] = arr[i+arr.length/2];
}
console.log(data);

这篇关于我正在将表数据作为字符串数组获取,如何将它们转换为jsons数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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