convert url encoded data to JSON array or js array - javascript

How can I convert data that looks like it is url encoded to a JSON array or js array.
&nfl_s_delay=120&nfl_s_stamp=1109033755&nfl_s_left1=Cleveland%2024%20%20%20Cincinnati%203%20(END%20OF%204TH)&nfl_s_right1_count=0&nfl_s_url1=http://sports.espn.go.com/nfl/boxscore?gameId=400554328&nfl_s_left2=Kansas%20City%2017%20%20%20Buffalo%2013%20(00:00%20IN%204TH)&nfl_s_right2_count=0&nfl_s_url2=http://sports.espn.go.com/nfl/boxscore?gameId=400554332&nfl_s_left3=Miami%2016%20%20%20Detroit%2020%20(00:00%20IN%204TH)&nfl_s_right3_count=0&nfl_s_url3=http://sports.espn.go.com/nfl/boxscore?gameId=400554355&nfl_s_left4=^Dallas%2031%20%20%20Jacksonville%2017%20(FINAL)&nfl_s_right4_count=0&nfl_s_url4=http://sports.espn.go.com/nfl/boxscore?gameId=400554358&nfl_s_left5=San%20Francisco%2027%20%20%20New%20Orleans%2024%20(END%20OF%201ST%20OT)&nfl_s_right5_count=0&nfl_s_url5=http://sports.espn.go.com/nfl/boxscore?gameId=400554362&nfl_s_left6=Tennessee%207%20%20%20Baltimore%2021%20(00:00%20IN%204TH)&nfl_s_right6_count=0&nfl_s_url6=http://sports.espn.go.com/nfl/boxscore?gameId=400554367&nfl_s_left7=Pittsburgh%2013%20%20%20NY%20Jets%2020%20(END%20OF%204TH)&nfl_s_right7_count=0&nfl_s_url7=http://sports.espn.go.com/nfl/boxscore?gameId=400554370&nfl_s_left8=Atlanta%2027%20%20%20Tampa%20Bay%2017%20(END%20OF%204TH)&nfl_s_right8_count=0&nfl_s_url8=http://sports.espn.go.com/nfl/boxscore?gameId=400554372&nfl_s_left9=Denver%2041%20%20%20Oakland%2010%20(00:00%20IN%203RD)&nfl_s_right9_count=0&nfl_s_url9=http://sports.espn.go.com/nfl/boxscore?gameId=400554396&nfl_s_left10=St.%20Louis%2014%20%20%20Arizona%2010%20(00:00%20IN%202ND)&nfl_s_right10_count=0&nfl_s_url10=http://sports.espn.go.com/nfl/boxscore?gameId=400554397&nfl_s_left11=NY%20Giants%2017%20%20%20Seattle%2017%20(00:00%20IN%203RD)&nfl_s_right11_count=0&nfl_s_url11=http://sports.espn.go.com/nfl/boxscore?gameId=400554400&nfl_s_left12=Chicago%20at%20Green%20Bay%20(8:30%20PM%20ET)&nfl_s_right12_count=0&nfl_s_url12=http://sports.espn.go.com/nfl/preview?gameId=400554403&nfl_s_left13=Carolina%20at%20Philadelphia%20(8:30%20PM%20ET)&nfl_s_right13_count=0&nfl_s_url13=http://sports.espn.go.com/nfl/preview?gameId=400554408&nfl_s_count=13&nfl_s_loaded=true
I would split split by &nfl_s_left2= , but the parameter increases by one for each time. So I could split by &nfl_s_left2= but that would only split once as the next thing that is similar is &nfl_s_left3= . So I guess a more specific question is can I split by a variable string?
I could split by &nfl_s_left but then I'd get the number= in the value of the array.
What would be the best way I could clean up this data?

how about this
function url_split(str){
var jsArry={},tmp = str.split('&nfl_s_'),tmp2;
tmp.forEach(function(e){
tmp2 = e.split('=');
jsArry[tmp2[0]] = tmp2[1];
});
return jsArry;
}
and the fiddle demo http://jsfiddle.net/oswspkL0/

Related

how to add comma after each object and convert into array in javascript react js

how can i add comma after each object and convert into array. here's my response data
{"url":"example.com/john","age":"32"}{"url":"example.com/mike","age":"42"}
i want this
[{"url":"example.com/john","age":"32"},{"url":"example.com/mike","age":"42"}]
i tried with regex but it's converting data into string but i want to render this data
How about a little bit of Regex magic to make it valid JSON.
const res = '{"url":"example.com/john","age":"32"}{"url":"example.com/mike","age":"42"}';
const betterJson = `[${res.replace(/}{/g,'},{')}]`;
console.log(JSON.parse(betterJson))

Convert a string made from array toString() back to Array

I converted an array to a string and added it to a TextArea. The user edited the TextArea and I need to now update the array by calling in the same string of data I first produced. How would you do this?
The string which I have produced and need to convert back to an array is:
{"color":"red","x":218,"y":-11,"width":60,"height":60},{"color":"blue","cx":114,"cy":83,"radius":30}
I tried to use the JSON Parser JSON.parse(text)
Format your string:
const text = '{"color":"red","x":218,"y":-11,"width":60,"height":60},{"color":"blue","cx":114,"cy":83,"radius":30}'
console.log(JSON.parse(`[ ${text}]`))
You just need to format your string as an array in JSON format. You can do that like so:
JSON.parse('[' + text + ']')
The Below code should work:
var text_string = '{"color":"red","x":218,"y":-11,"width":60,"height":60},{"color":"blue","cx":114,"cy":83,"radius":30}';
console.log(JSON.parse(`[${text_string}]`));

Add comma after each number in an array

I'm trying to create a simple page that prompts the user for an input and checks a list to see if that input is included and will then return a true or false answer to the user. At the moment it is working fine and the code looks like this:
var barcodes = [
123425245,
62753264574,
1878769047,
00000000000001718075,
302,
303,
];
var entry=prompt("enter barcode");
var num1=parseInt(entry);
document.write(barcodes.includes(num1));
My problem is that I have over 2000 items to add to this array and I am trying to copy and paste them in from an excel spreadsheet. When I copy them in there is no comma separating each barcode and therefore i'm getting an error message. Is there a quick way to add commas at the end of each number in the array or do I have to do it manually?
Thanks for any help
As from Docs:
The split() method splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.
You can create an array using split() by splitting your data like this:
let str = "123425245 62753264574 1878769047 00000000000001718075 302 303";
let barcodes = str.split(" ").map(Number);
console.log(barcodes);
var barcodes = [
123425245,
62753264574,
1878769047,
00000000000001718075,
302,
303,
];
var temp = barcodes.join(",");
console.log(temp)

How to get value after a particular string in an array in javascript

I have a json data in this format.
({test:[test, "<br/><div><b>Name</b>&nbsp;&nbsp;&nbsp; James;/div><div><b>Company</b>&nbsp;&nbsp;&nbsp;Company1</div><div><b>Phone.....}).
I need to get the value after 'Company' using javascript, is there any possibilities....
Please help me on this....
assuming your data was in some sort of parsable format, you could do this:
var textAfterCompany = data.split('Company')[1];
split returns an array of strings, [0] is everything to the left and [1] is everything to the right (assuming there is only one occurrence of 'company')
var text = test[1].substring(test[1].indexOf("Company"), test[1].length);

Comparing and splitting of string inside JavaScript function

I have a string in a variable:
var test= "http://www.gmail.com#%#http://www.google.com#%#http://www.yahoo.com#%#";
I want to split this string at the occurrences of the special characters, ie: #%#, then after splitting I want to push this thing to an array like this:
var spcds = [];
spcds.push("http://www.gmail.com");
spcds.push("http://www.google.com");
spcds.push("http://www.yahoo.com");
What I need is just split the string variable and push that to the spcds array. How can I do this in my JavaScript function such that the resultant values will be stored to another variable which I then push to the array spcds.
Use the split method to split the string:
var parts = test.split("#%#");
And if you don’t want to have empty parts, use the filter method to filter out the values that are not an empty string:
var parts = test.split("#%#").filter(function(val){return val!="";});
This will give you an array of the strings you are after Then you can just iterate through the array as needed.
var mySplitResult = myString.split("#%#");
this should give you the array you want:
var testarray=
("http://www.gmail.com#%#http://www.google.com#%#http://www.yahoo.com#%#")
.split('#%#');

Categories

Resources