How do I make Array.indexOf() case insensitive? - javascript

I am making a piece of code for a website that will have a list of names in an array and pick a random name, I want to add a feature that will let the user add or delete a name from the array. I have all of these features but when deleting a name, the user has to type the name to match the Case in the array. I tried to make the so it would be Case-Insensitive, what am I doing wrong?
<html>
<!--Other code uneeded for this question-->
<p id="canidates"></p>
<body>
<input type="text" id="delname" /><button onclick="delName()">Remove Name from List</button>
<script>
//Array of names
var names = [];
//Other code uneeded for this question
//List of Canidates
document.getElementById('canidates').innerHTML =
"<strong>List of Canidates:</strong> " + names.join(" | ");
//Other code uneeded for this question
//Remove name from Array
function delName() {
var dnameVal = document.getElementById('delname').value;
var pos = names.indexOf(dnameVal);
var namepos = names[pos]
var posstr = namepos.toUpperCase();
var dup = dnameVal.toUpperCase();
if(dup != posstr) {
alert("Not a valid name");
}
else {
names.splice(pos, 1);
document.getElementById('canidates').innerHTML =
"<strong>List of Canidates:</strong> " + names.join(" | ");
}
}
</script>
</body>
</html>

ES2015 findIndex:
var array = ['I', 'hAve', 'theSe', 'ITEMs'],
indexOf = (arr, q) => arr.findIndex(item => q.toLowerCase() === item.toLowerCase());
console.log( indexOf(array, 'i') ) // 0
console.log( indexOf(array, 'these') ) // 2
console.log( indexOf(array, 'items') ) // 3

In ECMA-262, 5th edition, you could use Array.prototype.some for this.
var array = [ 'I', 'hAve', 'theSe', 'ITEMs' ];
var query = 'these'.toLowerCase();
var index = -1;
array.some(function(element, i) {
if (query === element.toLowerCase()) {
index = i;
return true;
}
});
// Result: index = 2

Easy way would be to have a temporary array that contains all the names in uppercase. Then you can compare the user input. So your code could become somthing like this:
function delName() {
var dnameVal = document.getElementById('delname').value;
var upperCaseNames = names.map(function(value) {
return value.toUpperCase();
});
var pos = upperCaseNames.indexOf(dnameVal.toUpperCase());
if(pos === -1) {
alert("Not a valid name");
}
else {
names.splice(pos, 1);
document.getElementById('canidates').innerHTML =
"<strong>List of Canidates:</strong> " + names.join(" | ");
}
}
Hope this helps solve your problem.

The most elegant solution would be to convert the array into a string first, then do a case insensitive comparison. For example:
var needle = "PearS"
var haystack = ["Apple", "banNnas", "pEArs"];
var stricmp = haystack.toString().toLowerCase(); // returns
// "apple,bananas,pears"
if (stricmp.indexOf(needle.toLowerCase()) > -1) {
// the search term was found in the array
} else {
// the search term was not found in the array
}

Probably best to create your own custom indexOf method, something like this.
'use strict';
var customIndexOf = function(arrayLike, searchElement) {
var object = Object(arrayLike);
var length = object.length >>> 0;
var fromIndex = arguments.length > 2 ? arguments[2] >> 0 : 0;
if (length < 1 || typeof searchElement !== 'string' || fromIndex >= length) {
return -1;
}
if (fromIndex < 0) {
fromIndex = Math.max(length - Math.abs(fromIndex), 0);
}
var search = searchElement.toLowerCase();
for (var index = fromIndex; index < length; index += 1) {
if (index in object) {
var item = object[index];
if (typeof item === 'string' && search === item.toLowerCase()) {
return index;
}
}
}
return -1;
};
var names = [
'John',
'Anne',
'Brian'
];
console.log(customIndexOf(names, 'aNnE'));
Or even
'use strict';
var customIndexOf = function(array, searchElement, fromIndex) {
return array.map(function(value) {
return value.toLowerCase();
}).indexOf(searchElement.toLowerCase(), fromIndex);
};
var names = [
'John',
'Anne',
'Brian'
];
console.log(customIndexOf(names, 'aNnE'));
You may also want to add more checks to be sure that each element in the array is actually a String and that the searchElement is also actually a String too. If pre-ES5 then load appropriate shims

You can use Array.prototype.find()
found = myArray.find(key => key.toUpperCase() === searchString.toUpperCase()) != undefined;
Example:
myArray = ['An', 'aRRay', 'oF', 'StringS'];
searchString = 'array';
found = myArray.find(key => key.toUpperCase() === searchString.toUpperCase()) != undefined;
if (found ) {
// The array contains the search string
}
else {
// Search string not found
}
Note: Array cannot contain undefined as a value.

It is possible using by map method. For example see below code
var _name = ['prasho','abraham','sam','anna']
var _list = [{name:'prasho'},{name:'Gorge'}];
for(var i=0;i<_list.length;i++)
{
if(_name.map(function (c) {
return c.toLowerCase();
}).indexOf(_list[i].name.toLowerCase()) != -1) {
//do what ever
}else{
//do what ever
}
}
More info

I needed something similar to this where I needed compare two strings using includes and needed to be able to support both case and case insensitive searches so I wrote the following small function
function compare(l1: string, l2: string, ignoreCase = true): boolean {
return (ignoreCase ? l1.toLowerCase() : l1).includes((ignoreCase ? l2.toLowerCase() : l2));
}
Same principle could apply to indexOf as below
function indexOf(l1: string, l2: string, ignoreCase = true): number {
return (ignoreCase ? l1.toLowerCase() : l1).indexOf((ignoreCase ? l2.toLowerCase() : l2));
}
I know this is not specifically Array.indexOf but hope this helps someone out if the come across this post on their travels.
To answer the ops question though, you can apply this similarly to an array combined with this answer from #ULIT JAIDEE (the slight change to this was using the tilda character as a separator in case any of the array values contained spaces)
function compare(l1: any[], l2: any[], ignoreCase = true): boolean {
return (ignoreCase ? l1.join('~').toLowerCase().split('~') : l1).indexOf((ignoreCase ? l2.join('~').toLowerCase().split('~') : l2));
}
Again hope this helps.

Turn the array into a string separated by a delimiter, turn that string lowercase, and then split the string back into an array by the same delimiter:
function findIt(arr, find, del) {
if (!del) { del = '_//_'; }
arr = arr.join(del).toLowerCase().split(del);
return arr.indexOf(find.toLowerCase());
}
var arr = ['Tom Riddle', 'Ron Weasley', 'Harry Potter', 'Hermione Granger'];
var find = 'HaRrY PoTtEr';
var index = findIt(arr, find);
if (~index) {
alert('Found ' + arr[index] + '! :D');
} else {
alert('Did not find it. D:');
}

This is the shortest one.
haystack.join(' ').toLowerCase().split(' ').indexOf(needle.toLowerCase())

// unique only, removes latter occurrences
array.filter((v, i, a) => a.findIndex(j => v.toLowerCase() === j.toLowerCase()) === i);

To improve on #vsync answer and handle mixed content in the array here is my take. (I understand the OP is about case-sensitive thus it implies strings, maybe :)
var array = ['I', 'hAve', 7, {}, 'theSe', 'ITEMs'],
Contains = (arr, q) =>
arr.findIndex((item) => q.toString().toLowerCase() === item.toString().toLowerCase());
console.log(Contains(array, 'i'));
console.log(Contains(array, 'x'));
console.log(Contains(array, {} ));
console.log(Contains(array, 7 ));

You can't make it case-insensitive. I'd use an object instead to hold a set of names:
function Names() {
this.names = {};
this.getKey = function(name) {
return name.toLowerCase();
}
this.add = function(name) {
this.names[this.getKey(name)] = name;
}
this.remove = function(name) {
var key = this.getKey(name);
if (key in this.names) {
delete this.names[key];
} else {
throw Error('Name does not exist');
}
}
this.toString = function() {
var names = [];
for (var key in this.names) {
names.push(this.names[key]);
}
return names.join(' | ');
}
}
var names = new Names();
function update() {
document.getElementById('canidates').innerHTML = '<strong>List of Canidates:</strong> ' + names;
}
function deleteName() {
var name = document.getElementById('delname').value;
try {
names.remove(name);
update();
} catch {
alert('Not a valid name');
}
}
update();

Related

Find the replaced part in a string using Javascript

function str_replace(str , part_to_replace , replace_with) {
var res = str.replace(part_to_replace , replace_with);
return res;
}
console.log(str_replace("amir" , "ir" , "er")) //returns "amer"
I want the function to return "e" which is the only part that changed aka replaced part so how i am supposed to do that ?
thanks in advance.
You could iterate all characters and take only the changed ones.
function check(a, b) {
if (a.length !== b.length) { return; }
return b
.split('') // take an array
.filter(function (c, i) { // filter
return a[i] !== c; // check characters
})
.join(''); // return string
}
function str_replace(str, part_to_replace, replace_with) {
return str.replace(part_to_replace, replace_with);
}
console.log(str_replace("amir", "ir", "er"));
console.log(check("amir", str_replace("amir", "ir", "er")));
It looks like you want an array of characters in the new string that were not present in the old one. This will do the trick:
function getDifference(oldStr, newStr) {
// .split('') turns your string into an array of characters
var oldSplit = oldStr.split('');
var newSplit = newStr.split('');
// then compare the arrays and get the difference
var diff = [];
for (var i = 0; i < newSplit.length; i++) {
if (newSplit[i] !== oldSplit[i]) {
diff.push(newSplit[i]);
}
}
return diff;
}
var diff = getDifference('amir', str_replace('amir', 'ir', 'er'));
console.log(diff); // e

Algorithm for counting occurrences of words in string

I am trying to build an algorithm to see, which words occurr the most in comments.
Therefore I came up with this (in Javascript):
var analyze = function(comments){
var detectedWords = [];
var result = {};
comments.forEach(function(comment){
var words = comment.message.split(" ");
words.forEach(function(word){
word = word.toLowerCase();
if(word !== ""){
if(detectedWords.indexOf(word) === -1){
detectedWords.push(word);
result[detectedWords.indexOf(word)] = {"name":word,"count":1};
}else{
result[detectedWords.indexOf(word)].count++;
}
}
});
});
return _.orderBy(result, ['count'], ['desc']);
}
Can the algorithm be optimized further? (toLowerCase() outside the inner loop?
In next step I would define a "blacklist" or words that are not interesting like "the, is, I, am, are,..."
You could use a hash table for reference to the array item for a faster access to the count object. result is now an array, which is now sortable.
var analyze = function (comments) {
var result = [],
hash = {};
comments.forEach(function (comment) {
var words = comment.message.split(" ");
words.forEach(function (word) {
word = word.toLowerCase();
if (word !== "") {
if (!hash[word]) {
hash[word] = { name: word, count: 0 };
result.push(hash[word]);
}
hash[word].count++;
}
});
});
return result.sort(function (a, b) { return b.count - a.count;});
//return _.orderBy(result, ['count'], ['desc']);
}
console.log(analyze([{ message: 'a b c d a v d e f g q' }]));

Javascript sort alphabetically matching the beginning of string then alphabetically for contained text

I need help sorting through some data.
Say I type "piz" in a searchfield. I get in return and array with all the entries that contain "piz".
I now want to display them in the following order:
pizza
pizzeria
apizzetto
berpizzo
First the items that start with what I typed in alphabetical order then the ones that contain what I typed in alphabetical order.
Instead if I sort them alphabetically I get the following
apizzetto
berpizzo
pizza
pizzeria
Does anyone know how to do this?
Thanks for your help.
You can split the data into two arrays, one that starts with your input and one that doesn't. Sort each separately, then combine the two results:
var data = [
'pizzeria',
'berpizzo',
'apizzetto',
'pizza'
];
function sortInputFirst(input, data) {
var first = [];
var others = [];
for (var i = 0; i < data.length; i++) {
if (data[i].indexOf(input) == 0) {
first.push(data[i]);
} else {
others.push(data[i]);
}
}
first.sort();
others.sort();
return(first.concat(others));
}
var results = sortInputFirst('piz', data);
You can see it work here: http://jsfiddle.net/jfriend00/nH2Ff/
The right full solution is:
var data = [
'pizzeria',
'berpizzo',
'apizzetto',
'pizza'
];
var _sortByTerm = function (data, term) {
return data.sort(function (a, b) {
return a.indexOf(term) < b.indexOf(term) ? -1 : 1;
});
};
var result = _sortByTerm(data, 'piz');
If you want object sort, use this function:
var _sortByTerm = function (data, key, term) {
return data.sort(function (a, b) {
return a[key].indexOf(term) < b[key].indexOf(term) ? -1 : 1;
});
};
Using reduce:
const data = ['pizzeria', 'berpizzo', 'pizza', 'apizzetto'];
function sortInputFirst(input, data) {
data.sort();
const [first, others] = data.reduce(([a, b], c) => (c.indexOf(input) == 0 ? [[...a, c], b] : [a, [...b, c]]), [[], []]);
return(first.concat(others));
}
const output = sortInputFirst('piz', data);
console.log(output)
Here's another one:
var str = 'piz';
var arr = ['apizzetto','pizzeria','berpizzo','pizza'];
arr.sort(function(a,b) {
var bgnA = a.substr(0,str.length).toLowerCase();
var bgnB = b.substr(0,str.length).toLowerCase();
if (bgnA == str.toLowerCase()) {
if (bgnB != str.toLowerCase()) return -1;
} else if (bgnB == str.toLowerCase()) return 1;
return a < b ? -1 : (a > b ? 1 : 0);
});
console.log(arr);
Here's how I used the top voted answer to make my search function use a normalized set of data. It removes accents before comparing strings and is also case insensitive.
function getAutocompleteNormalizedMatches(userInput, array) {
const normalizedInput = getNormalizedString(userInput);
let normalizedListItem;
let startsWith = [];
let anywhere = [];
for (let i = 0; i < array.length; i++) {
normalizedListItem = getNormalizedString(array[i]);
if (normalizedListItem.indexOf(normalizedInput) === 0) {
startsWith.push(array[i])
} else if (normalizedListItem.includes(normalizedInput)) {
anywhere.push(array[i])
}
}
startsWith.sort();
anywhere.sort();
return startsWith.concat(anywhere);
}
const getNormalizedString = function (str) {
str = str.replace(/\s+/g, " ").trim();
return (str ? removeDiacritics(str.toLowerCase()) : "");
};
To get the removeDiacritics() function, please refer to this link because it takes quite a bit of useless space if you guys don't need it: Remove accents diacritics in a string with JavaScript

array_count_values for JavaScript instead

I have the following PHP-script, now I need to do the same thing in JavaScript. Is there a function in JavaScript that works similar to the PHP function, I have been searching for days but cannot find anything similar? What I want to do is to count the number of times a certain word is being used in an array.
$interfaceA = array($interfaceA_1,$interfaceA_2,$interfaceA_3,$interfaceA_4,$interfaceA_5,$interfaceA_6,$interfaceA_7,$interfaceA_8);
$interfaceA_array=array_count_values($interfaceA);
$knappsatsA = $interfaceA_array[gui_knappsats];
$touchpanelA = $interfaceA_array[gui_touchpanel];
Why not simply create a new javascript array "counts"
Iterate over original array, and increament the count of "counts" for keys encountered in the array.
http://jsfiddle.net/4t28P/1/
var myCurrentArray = new Array("apple","banana","apple","orange","banana","apple");
var counts = {};
for(var i=0;i< myCurrentArray.length;i++)
{
var key = myCurrentArray[i];
counts[key] = (counts[key])? counts[key] + 1 : 1 ;
}
alert(counts['apple']);
alert(counts['banana']);
Another elegant solution would be to use Array.prototype.reduce.
Given:
var arr = new Array("apple","banana","apple","orange","banana","apple");
You can just run reduce on it:
var groups =
arr.reduce(function(acc,e){acc[e] = (e in acc ? acc[e]+1 : 1); return acc}, {});
Finally you can check the result:
groups['apple'];
groups['banana'];
In the sample above reduce takes two parameters:
a function (anonymous here) taking an accumulator (initialized from the second argument of reduce), and the current array element
the initial value of the accumulator
Whatever the function returns, it will be used as the accumulator value in the next call.
From a type perspective, whatever the type of the array elements, the type of the accumulator must match the type of the second argument of reduce (initial value), and the type of the return value of the anonymous function.
This will also be the type of the return value of reduce.
Try
a.reduce((a,c)=> (a[c]=++a[c]||1,a) ,{});
let a= ["apple","banana","apple","orange","banana","apple"];
let count= a.reduce((a,c)=> (a[c]=++a[c]||1,a) ,{});
console.log(count);
How about this:
function arrayCountValues (arr) {
var v, freqs = {};
// for each v in the array increment the frequency count in the table
for (var i = arr.length; i--; ) {
v = arr[i];
if (freqs[v]) freqs[v] += 1;
else freqs[v] = 1;
}
// return the frequency table
return freqs;
}
let snippet = "HARRY POTTER IS A SERIES OF FANTASY NOVELS WRITTEN BY BRITISH AUTHOR J. K. ROWLING. THE NOVELS CHRONICLE" +
" THE LIVES OF A YOUNG WIZARD, HARRY POTTER , AND HIS FRIENDS HERMIONE GRANGER AND RON WEASLEY, ALL OF WHOM ARE " +
" STUDENTS AT HOGWARTS SCHOOL OF WITCHCRAFT AND WIZARDRY";
String.prototype.groupByWord = function () {
let group = {};
this.split(" ").forEach(word => {
if (group[word]) {
group[word] = group[word] + 1;
} else {
group[word] = 1;
}
});
return group;
};
let groupOfWordsByCount = snippet.groupByWord();
console.log(JSON.stringify(groupOfWordsByCount,null, 4))
This should work
function array_count_values(array) {
var tmpArr = {};
var key = '';
var t = '';
var _countValue = function(tmpArr, value) {
if (typeof value === 'number') {
if (Math.floor(value) !== value) {
return;
}
} else if (typeof value !== 'string') {
return;
}
if (value in tmpArr && tmpArr.hasOwnProperty(value)) {
++tmpArr[value];
} else {
tmpArr[value] = 1;
}
}
for (key in array) {
if (array.hasOwnProperty(key)) {
_countValue.call(this, tmpArr, array[key]);
}
}
return tmpArr;
}
console.log(array_count_values([12, 43, 12, 43, "null", "null"]));

How to convert an x-www-form-urlencoded string to JSON?

Exampple of application/x-www-form-urlencoded string
CorrelationId=1&PickedNumbers%5B%5D=1&PickedNumbers%5B%5D=2&PickedNumbers%5B%5D=3&PickedNumbers%5B%5D=4
Into JSON
var gamePlayData = {
CorrelationId: gameId,
PickedNumbers: ["1","2","3","4"]
};
This is a core module of Node.js now: https://nodejs.org/api/querystring.html#querystring_querystring_parse_str_sep_eq_options
var qs = require('querystring')
var json = qs.parse('why=not&sad=salad')
// { why: 'not', sad: 'salad' }
Works with encoded characters too:
var json2 = qs.parse('http%3A%2F%2Fexample.com&sad=salad')
// { url: 'http://example.com', sad: 'salad' }
I've been dealing with this recently: I had to parse data that could contain objects nested up to 5 levels deep. I needed the code to be able to deal with both rather complex data, but not fail to decode a URI as simple as id=213.
I spent quite some time on google, trying to find a (semi-)elegant solution to this problem, and this question kept showing up. Since it gets 1 view/day (give or take) I've decided to post my solution here, hope it helps someone out:
function form2Json(str)
{
"use strict";
var obj,i,pt,keys,j,ev;
if (typeof form2Json.br !== 'function')
{
form2Json.br = function(repl)
{
if (repl.indexOf(']') !== -1)
{
return repl.replace(/\](.+?)(,|$)/g,function($1,$2,$3)
{
return form2Json.br($2+'}'+$3);
});
}
return repl;
};
}
str = '{"'+(str.indexOf('%') !== -1 ? decodeURI(str) : str)+'"}';
obj = str.replace(/\=/g,'":"').replace(/&/g,'","').replace(/\[/g,'":{"');
obj = JSON.parse(obj.replace(/\](.+?)(,|$)/g,function($1,$2,$3){ return form2Json.br($2+'}'+$3);}));
pt = ('&'+str).replace(/(\[|\]|\=)/g,'"$1"').replace(/\]"+/g,']').replace(/&([^\[\=]+?)(\[|\=)/g,'"&["$1]$2');
pt = (pt + '"').replace(/^"&/,'').split('&');
for (i=0;i<pt.length;i++)
{
ev = obj;
keys = pt[i].match(/(?!:(\["))([^"]+?)(?=("\]))/g);
for (j=0;j<keys.length;j++)
{
if (!ev.hasOwnProperty(keys[j]))
{
if (keys.length > (j + 1))
{
ev[keys[j]] = {};
}
else
{
ev[keys[j]] = pt[i].split('=')[1].replace(/"/g,'');
break;
}
}
ev = ev[keys[j]];
}
}
return obj;
}
I've tested it, with data like the string below (4 levels deep):
str = "id=007&name[first]=james&name[last]=bond&name[title]=agent&personalia[occupation]=spy&personalia[strength]=women&personalia[weakness]=women&tools[weapons][close][silent]=garrot&tools[weapons][medium][silent]=pistol_supressed&tools[weapons][medium][loud]=smg&tools[weapons][far][silent]=sniper&tools[movement][slow]=foot&tools[movement][far]=DBS";
Which neatly returns an object, that, when passed through JSON.stringify comes out like this:
{"id":"007","name":{"title":"agent","first":"james","last":"bond"},"personalia":{"weakness":"women","occupation":"spy","strength":"women"},"tools":{"movement":{"far":"DBS","slow":"foot"},"weapons":{"close":{"silent":"garrot"},"medium":{"silent":"pistol_supressed","loud":"smg"},"far":{"silent":"sniper"}}}}
It passes a JSlint check, when ignoring white space, . and [^...] and accepting ++. All in all, I'd consider that to be acceptable.
You can use qs if you're using node, or browserify.
var qs = require('qs')
var encodedString = "CorrelationId=1&PickedNumbers%5B%5D=1&PickedNumbers%5B%5D=2&PickedNumbers%5B%5D=3&PickedNumbers%5B%5D=4"
console.log(qs.parse(encodedString))
// { CorrelationId: '1', PickedNumbers: [ '1', '2', '3', '4' ] }
the following code should do the trick:
var str = 'CorrelationId=1&PickedNumbers%5B%5D=1&PickedNumbers%5B%5D=2&PickedNumbers%5B%5D=3&PickedNumbers%5B%5D=4';
var keyValuePairs = str.split('&');
var json = {};
for(var i=0,len = keyValuePairs.length,tmp,key,value;i <len;i++) {
tmp = keyValuePairs[i].split('=');
key = decodeURIComponent(tmp[0]);
value = decodeURIComponent(tmp[1]);
if(key.search(/\[\]$/) != -1) {
tmp = key.replace(/\[\]$/,'');
json[tmp] = json[tmp] || [];
json[tmp].push(value);
}
else {
json[key] = value;
}
}
Updated answer for 2022, works both in the browser and in node.
Use URLSearchParams class.
Note: The param name PickedNumbers%5B%5D will turn to be the literal string PickedNumbers[]. You don't need to encode the brackets in order to make it an array
const paramsStr = 'CorrelationId=1&PickedNumbers%5B%5D=1&PickedNumbers%5B%5D=2&PickedNumbers%5B%5D=3&PickedNumbers%5B%5D=4';
const params = new URLSearchParams(paramsStr);
//access a specific param
console.log(params.get('PickedNumbers[]')); // '4'
console.log(params.getAll('PickedNumbers[]')); // ['1','2','3','4']
const o = Object.fromEntries(Array.from(params.keys()).map(k => [k, params.getAll(k).length===1 ? params.get(k) : params.getAll(k)]));
console.log(JSON.stringify(o)); //full object
Here's a pure-JavaScript way to do it. JavaScript frameworks might also help you out with this. EDIT: Just for kicks, I threw in dictionary parsing, too. See the 2nd example.
function decodeFormParams(params) {
var pairs = params.split('&'),
result = {};
for (var i = 0; i < pairs.length; i++) {
var pair = pairs[i].split('='),
key = decodeURIComponent(pair[0]),
value = decodeURIComponent(pair[1]),
isArray = /\[\]$/.test(key),
dictMatch = key.match(/^(.+)\[([^\]]+)\]$/);
if (dictMatch) {
key = dictMatch[1];
var subkey = dictMatch[2];
result[key] = result[key] || {};
result[key][subkey] = value;
} else if (isArray) {
key = key.substring(0, key.length-2);
result[key] = result[key] || [];
result[key].push(value);
} else {
result[key] = value;
}
}
return result;
}
decodeFormParams("CorrelationId=1&PickedNumbers%5B%5D=1&PickedNumbers%5B%5D=2&PickedNumbers%5B%5D=3&PickedNumbers%5B%5D=4");
// => {"CorrelationId":"1","PickedNumbers":["1","2","3","4"]}
decodeFormParams("a%5Bb%5D=c&a%5Bd%5D=e");
// => {"a":{"b":"c","d":"e"}}
Try this->
// convert string to object
str = 'a=6&id=99';
var arr = str.split('&');
var obj = {};
for(var i = 0; i < arr.length; i++) {
var bits = arr[i].split('=');
obj[bits[0]] = bits[1];
}
//alert(obj.a);
//alert(obj.id);
// convert object back to string
str = '';
for(key in obj) {
str += key + '=' + obj[key] + '&';
}
str = str.slice(0, str.length - 1);
alert(str);
Or use this (JQuery) http://api.jquery.com/jQuery.param/
A one-liner:
let s = 'a=1&b=2&c=3';
Object.fromEntries(
s.split('&')
.map(s => s.split('='))
.map(pair => pair.map(decodeURIComponent)))
// -> {a: "1", b: "2", c: "3"}
and if you want repeated parameters to be represented as arrays:
let s = 'a=1&b=2&c[]=3&c[]=4&c[]=5&c[]=6';
s
.split('&')
.map(s => s.split('='))
.map(pair => pair.map(decodeURIComponent))
.reduce((memo, [key, value]) => {
if (!(key in memo)) { memo[key] = value; }
else {
if (!(memo[key] instanceof Array))
memo[key] = [memo[key], value];
else
memo[key].push(value);
}
return memo;
}, {})
// -> {"a":"1","b":"2","c[]":["3","4","5","6"]}
You need the opposite of jQuery.param. One of the options is http://benalman.com/code/projects/jquery-bbq/examples/deparam/
var jsonMessage = "{\"".concat(message.replace("&", "\",\"").replace("=", "\":\"")).concat("\"}");
In typescript, works for me:
Use qs.parse to transform in object ParsedQs.
Use as unknow to implicit type unknow and before force convert to string.
Use JSON.parse to convert an string to object.
It was useful to use validations with Joi.
const payload = JSON.parse(JSON.stringify(qs.parse(request.body) as unknown as string));
Payload (cURL):
--data-urlencode 'notification=123-456123' \
--data-urlencode 'test=123456' \
--data-urlencode 'ajieoajeoa=Lorem ipsum'
Result:
{
notification: '123-456123',
test: '123456',
ajieoajeoa: 'Lorem ipsum'
}
public static void Main()
{
string str ="RESULT=0&PNREF=A10AABBF8DF2&RESPMSG=Approved&AUTHCODE=668PNI&PREFPSMSG=No Rules Triggered&POSTFPSMSG=No Rules Triggered";
var sr = str.Replace("&", "=");
string[] sp = sr.Split('=');
var spl = sp.Length;
int n = 1;
var ss = "{";
for (var k = 0; k < spl; k++)
{
if (n % 2 == 0)
{
if (n == spl)
{
ss += '"' + sp[k] + '"';
}
else
{
ss += '"' + sp[k] + '"' + ",";
}
}
else
{
ss += '"' + sp[k] + '"' + ":";
}
n++;
}
ss += "}";
Console.WriteLine(ss);
}

Categories

Resources