Reputation: 13
Use Case : I need the corresponding values from a CSV file to be displayed in the console based on the unique country which is selected.
I have Tried :- 1.)I tried converting the CSV file to an array using: xmlData=xmlhttp.responseText
var dataArr = xmlData.split("\n");
var heading = dataArr[0].split(",");
var data = dataArr.splice(1, dataArr.length - 1);
Below is the output of the array and I need to convert the below array to an JSON object so that if m giving switz as a key the corresponding values which is "5/19/2019 20:49:57,Try it Yourself �,mousedown" should be displayed in the console.
[
"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"]
Please help me to find an solution for this.
Upvotes: 1
Views: 259
Reputation: 3234
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"])
Upvotes: 0
Reputation: 4836
try this:
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)
Upvotes: 0
Reputation: 66
I not sure that i understand you right, but if you want to convert this array to object which country as key and rest as value, then i write a bit of code for you :3 Also i apply JSON.stringify, if you need not javascript but json object.
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));
Output:
/*{
"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}
}*/
Upvotes: 0
Reputation: 126
try this
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);
output
{
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"
}
Upvotes: 0
Reputation: 5422
I think this is what you're looking for:
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 ) );
Upvotes: 0
Reputation: 693
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
Upvotes: 0
Reputation: 1350
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)
Upvotes: 1