Parsing Javascript cookie values and separating values - javascript

I'm still learning Javascript and I'm trying to parse a value from a cookie in a specific key:
I'm using this current line:
const cookieObj = new URLSearchParams(document.cookie.replaceAll("; ","&","|"))
cookieObj.get("LG_AFF_TRK")
LG_AFF_TRK is the key that I'm pulling the value from. Doing so creates the output value of the following:
1|2|3|4|5|6
Each number represented would be a value that will have a random number of characters -and- could possibly be empty but each section always be separated by the "|" symbol.
How can I pull a specific value? As an example question, what do I need to do pull value 3?
I'm trying to understand this logically and my guess is I have to create an array first?

Related

How to extract data value that is nested inside an array

I am just trying to figure out why when trying to grab a value through the dot notation, it is giving me undefined error. Please see below for what I mean.
I am trying to extract the value from the nested object in the headline color array:
If we can assume that el.selectgroup1 is equal to some string value "[{name: 'Off White', value: 'off-white'}]". then we can deduce that el.selectgroup1[0] is equal to "[". which would not be valid JSON within JSON.parse("[").
Try something like JSON.parse(el.selectgroup1)[0].value

React how to address a key from string name

Ok, so I have a database with data sets in it. the application performs a base API call to retrieve that base data set containing all other data sets. I will receive a string variable with the name of the key I need to access so let's say const addon = "Book". However, I don't know the key name beforehand. So the following code works but I need to somehow not hard code the key parameter but rather use the string value incoming from the const addon. I am not quite sure how to do this please point me to the right documentation or explain how to achieve the wanted result.
const columns = levelOne.Book && Object.keys(levelOne.Book);
However, as the incoming param might not be "Book" but anything else it will only work for this case. It is guaranteed that there is a key-value pair where the key bears the name stored in the string value of addon.
You can use a variable as the key. For example, levelOne[variable] where variable is the string that you want to use as the key.
Also, you can get the keys through Object.keys(levelOne) and then you can set variable value from the keys array.

Problem creating localStorage key dynamically using template literals

I am trying to save values to localStorage with keys that are created using template literals to create new a new string from an existing string and a variable as the key, and concatenation a string and a number as the value.
In the console, the strings appear to be formatted correctly for local storage, but when run it produces the error "
TypeError: Failed to execute 'setItem' on 'Storage': 2 arguments required, but only 1 present."
*The snippet below throws a sandbox error rather than the one I receive in my local environment. Can anyone shed some light at what I'm doing wrong?
var tm="Charlotte Knights";
var cl ="AA";
var yr = 2019;
console.log(`"my${cl}", "${tm}_${yr}"`)
localStorage.setItem(`"my${cl}", "${tm}_${yr}"`)
localStorage.setItem(`"my${cl}", "${tm}_${yr}"`) passes one string to the setItem function. To pass two, you need to end the template literal prior to the comma, then start another after it:
localStorage.setItem(`my${cl}`, `${tm}_${yr}`)
// --------------------------^--^
Also get rid of the " within the templates, unless you want actual " characters in the key and value.

NaN When trying to print json values

I was trying to parse a json which I got as a response of querying connections in linked in.
when I do JSON.stringify in an array as a whole I can see values in console.log
but when I try to take individual values inside array I get NaN.
Why can I not get Individual values when I can see the array as a whole.
here is the code
var response = jQuery.parseJSON(data);
var person = response.person[0];
in the above code I am getting data as a response of an ajax call
person is an array inside, I can stringify the array as a whole.
if I do
console.log(JSON.stringify(person));
I will get
{"id":"someId","first-name":"someName","last-name":"someName, DMC-E, DMC-D","picture-url":"https://soempicture"}
but When I try to take it individually
console.log(person.first-name);
I get NaN , and trying to strigify it results in Null
am I missing something, should I do string split to get the values?
Thank you
You can't access the first-name property using period notation, as the name contains a dash.
The code will be interpreted as person.first - name, i.e. the person.first property minus the name variable.
Use the bracket notation for any property where the name can't be an identifier:
console.log(person['first-name']);
To access a key that contains characters that cannot appear in an identifier (-), use brackets, i.e.:
person["first-name"]

Creating a better JSON format

I am returning a query object from Coldfusion as a JSON string which I then parse into JSON in Javascript. It has a bit of a strange format when I finally log it though.
I am faced with two problems. First, I do not know how to access the lowest element (i.e Arthur Weasley) as I cannot use a number in my selector (response.DATA[0].0 doesn't work because the lowest field name is a number). Second, is there any way to assign the values in the columns section to the fields that are numbered 1, 2 and 3?
What I'm really asking is how do I select my lowest level of data? If that can't be done because of the numbers for field names, how do I change the names to something more fitting?
My data logged:
First entry of first entry of DATA = response.DATA[0][0]
So
name = reponse.DATA[0][0];
trainsThing = response.DATA[0][1];

Categories

Resources