需要将数组转换为对象

时间:2019-05-24 22:52:34

标签: javascript

用例: 我需要根据所选的唯一国家/地区将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对象,以便如果m以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"]

请帮助我找到解决方案。

7 个答案:

答案 0 :(得分:0)

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)

答案 1 :(得分:0)

我认为这是您想要的:

const array = [
  "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",
];

let result = {};
for ( let element of array ) {
  let key = element.split( "," )[ 3 ];
  let split = element.split( "," );
  split.splice( -1, 1);

  element = split.join();
  
  result[ key ] = element;
}

console.log( JSON.stringify( result ) );

答案 2 :(得分:0)

我不确定我是否理解正确,但是如果您想将此数组转换为对象,哪个国家作为键,其余国家作为值,那么我为您编写了一些代码:3 如果您不需要JavaScript,但我需要JSON对象,我也会应用JSON.stringify。

let result = {}
const arr = ["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"]


for (let item of arr) {
    let temp = item.split(',');
    let key = temp.splice(3, 1);
    result[key] = temp.join(',');
}

console.log(JSON.stringify(result, null, 4));

输出:

/*{
    "switz": "5/19/2019 20:49:57,Try it Yourself �,mousedown",
    "africa": "5/21/2019 4:53:59,Try it Yourself �,mousedown",
    "spider": "5/19/2019 21:42:13,Try it Yourself �,mousedown"
    {...more items}
}*/

答案 3 :(得分:0)

var data = [ // This's the data Array you should edit it with your above code !

  "Adnane",
  "Aref"
  
];

let jsonObj=JSON.stringify(data);

document.body.innerHTML=jsonObj; //Prints the first index of the JSON Object on the html body

答案 4 :(得分:0)

尝试

let arr = ["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"];

function toObject(arr) {
  var rv = {};
  for (var i = 0; i < arr.length; ++i)
    rv[i] = arr[i];
  return rv;
}
toObject(arr);

输出

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

答案 5 :(得分:0)

尝试一下:

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"];

const result = response.reduce((a, ele) => {
    a[ele.split(',').slice(-1)] = ele.split(',').slice(0, -1).join(',')
    return a
}, [])

console.log(result)

答案 6 :(得分:0)

let y = JSON.parse('["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"]');
let mapArr = y.reduce((acc, obj) => { 
  var valuesArr = obj.split(",");
  let key = valuesArr.pop();
  acc[key] = valuesArr.join(',');
  return acc;
}, new Map());

console.log(mapArr["switz"])