gpt4 book ai didi

javascript - 需要将数组转换为对象

转载 作者:行者123 更新时间:2023-11-30 19:32:19 25 4
gpt4 key购买 nike

用例:我需要根据所选的唯一国家/地区将 CSV 文件中的相应值显示在控制台中。

我试过了:-1.)我尝试使用以下方法将 CSV 文件转换为数组:xmlData=xmlhttp.responseText

  var dataArr = xmlData.split("\n");
var heading = dataArr[0].split(",");
var data = dataArr.splice(1, dataArr.length - 1);

下面是数组的输出和我需要将下面的数组转换为一个 JSON 对象,这样如果我将 switz 作为键,则相应的值“5/19/2019 20:49:57,自己试试 �,mousedown”应该显示在控制台中.

[
"5/19/2019 20:49:57,Try it Yourself �,mousedown,switz
", "5/19/2019 21:38:49,Try it Yourself �,mousedown,africa
", "5/19/2019 21:42:13,Try it Yourself �,mousedown,spider
", "5/19/2019 21:45:21,Try it Yourself �,mousedown,san jose
", "5/19/2019 21:48:25,Try it Yourself �,mousedown,san francisco
", "5/19/2019 21:53:27,Try it Yourself �,mousedown,Los Angeles
", "5/19/2019 22:20:16,Try it Yourself �,mousedown,san diego
", "5/19/2019 23:37:36,Try it Yourself �,mousedown,dubai
", "5/20/2019 0:23:51,Try it Yourself �,mousedown,new york
", "5/21/2019 4:04:53,Try it Yourself �,mousedown,denmark
", "5/21/2019 4:08:13,Try it Yourself �,mousedown,denmark
", "5/21/2019 4:09:52,Try it Yourself �,mousedown,germany
", "5/21/2019 4:11:34,Try it Yourself �,mousedown,delhi
", "5/21/2019 4:15:46,Try it Yourself �,mousedown,africa
",
"5/21/2019 4:53:59,Try it Yourself �,mousedown,africa
", "5/21/2019 4:55:08,Try it Yourself �,mousedown,denamrk
",
"5/23/2019 4:21:51,Try it Yourself �,mousedown,santa barbara
", "5/23/2019 5:59:00,Try it Yourself �,mousedown,santa monica"]

请帮我找到解决方案。

最佳答案

const response = [ "5/19/2019 20:49:57,Try it Yourself �,mousedown,switz ", "5/19/2019 21:38:49,Try it Yourself �,mousedown,africa ", "5/19/2019 21:42:13,Try it Yourself �,mousedown,spider ", "5/19/2019 21:45:21,Try it Yourself �,mousedown,san jose ", "5/19/2019 21:48:25,Try it Yourself �,mousedown,san francisco ", "5/19/2019 21:53:27,Try it Yourself �,mousedown,Los Angeles ", "5/19/2019 22:20:16,Try it Yourself �,mousedown,san diego ", "5/19/2019 23:37:36,Try it Yourself �,mousedown,dubai ", "5/20/2019 0:23:51,Try it Yourself �,mousedown,new york ", "5/21/2019 4:04:53,Try it Yourself �,mousedown,denmark ", "5/21/2019 4:08:13,Try it Yourself �,mousedown,denmark ", "5/21/2019 4:09:52,Try it Yourself �,mousedown,germany ", "5/21/2019 4:11:34,Try it Yourself �,mousedown,delhi ", "5/21/2019 4:15:46,Try it Yourself �,mousedown,africa ", "5/21/2019 4:53:59,Try it Yourself �,mousedown,africa ", "5/21/2019 4:55:08,Try it Yourself �,mousedown,denamrk ", "5/23/2019 4:21:51,Try it Yourself �,mousedown,santa barbara ", "5/23/2019 5:59:00,Try it Yourself �,mousedown,santa monica"];


let obj = {};
for(let r of response) {
let arry = r.split(',');
let key = arry.pop().trim()
obj[key] = arry.join(',');
}

console.log(obj.switz)

关于javascript - 需要将数组转换为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56300282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com