PLEASE CONSIDER REMOVING THE DUPLICATE QUESTION FLAG. IT IS NOT A DUPLICATE QUESTION BECAUSE IT IS NOT ASKING HOW TO MODIFY STRINGS BUT ADDRESSES WHAT MAY BE A BUG RESULTING IN ERRORS BEING THROWN WHEN STRING METHODS ARE APPLIED TO STRINGS CORRECTLY. THE MISUNDERSTANDING MAY BE DUE TO NOT READING PAST THE SPECIFIC EXAMPLE I USED TO ILLUSTRATE THE PROBLEM (WHICH WAS REMOVING QUOTE MARKS FROM A STRING) BUT IS NOTHING TO DO WITH THE SPECIFICS OF THAT EXAMPLE. RATHER IT IS AN UNEXPECTED ERROR WHEN ANY STRING METHOD IS APPLIED TO ANY STRING IN THE PARTICULAR CIRCUMSTANCES DISCUSSED. THANKS.
(I know how to truncate or replace strings, string methods are not working here. I also know how to remove quotes from strings, this is NOT a repeat of the suggested question. While I appreciate help, please only answer if you read past the bit where I say I want to remove the quotes. I want to know why I get an error when I am using string methods correctly)
I have an array containing sets of data. A typical set (i.e. array element) is a string containing data pairs separated with commas. So the first element might look like this:
sets[0] = '"name":"dave","height":"tall","age":"old"'
Each element holds a string, not an array (and I have confirmed this by printing typeof for each element).
I process each element to form an array of its component parts by splitting at the commas:
let currentSet = sets[i].split(",");
so, for the sets[0] example, set[0] fills currentSet to be:
currentSet = ['"name":"dave"', '"height":"tall"', '"age":"old"']
and
currentSet[0] = '"name":"dave"';
I now want to extract the "dave" portion of each current set and push it to an new results array:
dat = currentSet[j].split(":")[1];
resArray.push(dat);
This works as expected and I get '"dave"' as a new element in the resArray.
The next part is proving difficult. Instead of pushing "dave" (with the literal quote marks) to the array, I want to push dave (without quote marks being in the string) to the array so instead of:
resArray = ['"dave"', '"tall"', '"old"']
I want:
resArray = ['dave', 'tall', 'old']
I have tried using dat.slice dat.substring and dat.replace to modify the string pushed to the array but all throw an error (can't apply string methods to null).
I'm not especially asking for solutions of how to achieve the result I want (although would welcome the neat solution that I have probably overlooked) but really want to understand why I cannot seem to process dat by any string method and push the result to an array. I should stress again that I have confirmed using typeof that each dat extraction is a string. This is driving me nuts. Any explanation would be most welcome.
Thanks.
My actual javascript lines (with the slice example) which processes each set of starting data:
for (var i = 0; i < sets.length; i++)
{
let currentSet = sets[i].split(",");
// e.g. sets[i] = '"name":"dave","height":"tall","age":"old"'
// e.g. currentSet = ['"name":"dave"', '"height":"tall"', '"age":"old"']
resArray[i] = [];
for (var j = 0; j < currentSet.length; j++)
{
dat = currentSet[j].split(":")[1];
resArray[i].push(dat.slice(1,dat.length-2)); // throws error
// resArray[i].push(dat) // works fine;
// resArray[i].push(typeof dat) // confirms dat type is string;
} // next subset j;
outArray[i] = resArray[i].join(",");
currentSet = null;
} // next sets i;