javascript split String at quotation marks // Json.parse gives undefined object - javascript

I have a string with this value:
[{"email":"SharePoint.Admin_SA.external#test.net","id":"i:0#.w|opmain\\xespsa","label":"Admin_SA, SharePoint","title":"SharePoint Projektteam SP","type":"User","value":"i:0#.w|opmain\\xespsa"}]
The strings all have the same format. It can also be
[{"email":"SharePoint.Admin#test.net","id":"i:0#.w|opmain\\xespec","label":"Admin, SharePoint","title":"SharePoint Projektteam SP","type":"User","value":"i:0#.w|opmain\\xespec"}]
But I need a "global" approach, so that it always splits it at label. For example, splitting the first one should only output Admin_SA, SharePoint.
How can one achieve this?
I tried the following, but it didn't give me the result I wanted:
var test = NWF$("#" + varRequestor).val();
var array = test.split(':');
var a = array[0];
var b = array[1];
var c = array[2];
var d = array[3];
var e = array[4];
console.log("a: " + a);
console.log("b: " + b);
console.log("c: " + c);
console.log("d: " + d);
console.log("e: " + e);
I tried using JSON.parse as well, but it didn't work:
console.log("Requestor DispName: " + NWF$("#" + varRequestor).val());
var obj = NWF$("#" + varRequestor).val();
const test = JSON.parse(obj);
console.log("obj.label:" + obj.label);

i think, you want value corresponding to label. you can try this.
let temp = [{"email":"SharePoint.Admin_SA.external#test.net","id":"i:0#.w|opmain\\xespsa","label":"Admin_SA, SharePoint","title":"SharePoint Projektteam SP","type":"User","value":"i:0#.w|opmain\\xespsa"}];
console.log(temp[0].label);

Related

How to get rid of undefined?

When I console.log the array below, undefined is a part of each array item, I would like to know why is that and how to get rid of it?
new_colors.addEventListener("click", function() {
var rgb_guesses = new Array();
for(var i = 0; i < color_squares.length; i++) {
var rgb_value = "rgb" + "(" + random() + "," + " " + random() + "," + " " + random() + ")";
color_squares[i].style.background = rgb_value;
rgb_guesses[i] += rgb_value;
}
guess_rgb.textContent = color_squares[3].style.background;
console.log(rgb_guesses);
});
You've created an empty array
So, the statement
rgb_guesses[i] += rgb_value;
is shorthand for
rgb_guesses[i] = rgb_guesses[i] + rgb_value;
Now as the array is empty, rgb_guesses[i] will be undefined until you assign a value to it ... undefined when coerced to a string, is "undefined"
So, your code is doing the equivalent of
rgb_guesses[i] = "undefined" + rgb_value;
Since you are only ever assigning a single value to each element in the array, you can change your code to simply
rgb_guesses[i] = rgb_value;
Or
rgb_guesses.push(rgb_value);

Renaming/Modifying using current name in javascript

I am new to javascript, i tried to modify a text shown below with substring command.
eg. "ABCD_X_DD_text" into "ABCD-X(DD)_text" this
i used this
var str = "ABCD_X_DD_cover";
var res = str.substring(0,4)+"-"+str.substring(5,6)+"("+str.substring(7,9)+")"+str.substring(9,15);
// print to console
console.log(res);
i got what i want. But problem is X and DD are numerical (digit) value. and they are changeable. here my code just stop working.
it can be ..... "ABCD_XXX_DDDD_text" or "ABCD_X_DDD_text".
could you suggest some code, which works well in this situation.
You can use a split of the words.
var strArray = [
"ABCD_X_DD_cover",
"ABCD_XX_DD_cover",
"ABCD_XXX_DD_cover"
];
for(var i = 0; i < strArray.length; i++){
var split = strArray[i].split("_");
var str = split[0] + "-" + split[1] + "(" + split[2] + ") " + split[3];
console.log(str);
}
I used a for cycle using an array of strings, but you can do it with a variable too.

javascript Function string.slice

I'm trying to do this following javascript exercise here:
Create a function called mixUp. It should take in two strings, and return the concatenation of the two strings (separated by a space) slicing out and swapping the first 2 characters of each. You can assume that the strings are at least 2 characters long.
and here's my code:
var mixUP = function(a, b) {
var sliceA = a.slice(0,2);
var sliceAa = a.slice(2);
var sliceB = b.slice(0,2);
var sliceBb = b.slice(2);
var string = sliceA + sliceBb + " " + sliceB + sliceAa;
console.log(string);
};
mixUp(apple, pear);
Could anyone please help me out here coz it's not working for me. Thanks heaps!
The way I approached it was:
function mixUp(stringA, stringB) {
var sliceA = stringA.slice(0,2),
sliceB = stringB.slice(0,2);
return (sliceB + stringA.substring(2) + " " + sliceA +
stringB.substring(2));
}
Which gives you the desired output
If you define mixUP call it, not mixUp last p is uppercase.
When you use strings, you need to add quotes around them:
var mixUp = function (a, b) {
var sliceA = a.slice(0, 2);
var sliceAa = a.slice(2);
var sliceB = b.slice(0, 2);
var sliceBb = b.slice(2);
var string = sliceA + sliceBb + " " + sliceB + sliceAa;
console.log(string);
};
mixUp('apple', 'pear');
OUTPUT
apar peple
Typo's:
mixUp(apple, pear);
should be:
mixUP('apple', 'pear');
^ ^ ^ ^ ^
You called mixUp instead of mixUP, and both apple and pear should be strings (Unless they are variables already)
That'll give you the desired result:
var mixUP = function(a, b) {
var sliceA = a.slice(0,2);
var sliceAa = a.slice(2);
var sliceB = b.slice(0,2);
var sliceBb = b.slice(2);
var string = sliceA + sliceBb + " " + sliceB + sliceAa;
console.log(string);
};
mixUP('apple', 'pear');
Either change the function calling or the function definition to same name as you have taken different names for both.
var mixUP = function(a, b) {
var sliceA = a.slice(0,2);
var sliceAa = a.slice(2);
var sliceB = b.slice(0,2);
var sliceBb = b.slice(2);
var string = sliceA + sliceBb + " " + sliceB + sliceAa;
console.log(string);
};
mixUP("apple", "pear"); // apar peple

I am getting characters rather than words when I alert an array reference

On the first alert(array[0]) I get a full word. On the next alert after the loop, I get only a character with each reference. I found a similar question on here, but there wasn't an answer with it.
var listNumbers = document.getElementById("someNames").getElementsByTagName("li");
for(var i = 0; i<listNumbers.length; i++) {
z = (listNumbers[i].innerHTML);
array = z.split(" ");
alert(array[0]);
firstArray = firstArray + '"' + array[0] + '"' + ", ";
lastArray = lastArray + '"' + array[1] + '"' + ", ";
phoneArray = phoneArray + '"' + array[2] + '"' + ", ";
}
alert(firstArray[1]);
You are building your firstArray variable as a string, not an array. When you access a string with [0], you will get the character at that position in the string, not a whole word.
If you want to be able to access words, you need to declare it as an array and use .push() to add words to it.
var listNumbers = document.getElementById("someNames").getElementsByTagName("li");
var arrayOfFirstNames = [];
var arrayOfLastNames = [];
var arrayOfPhoneNumbers = [];
for(var i = 0; i<listNumbers.length; i++) {
z = (listNumbers[i].innerHTML);
array = z.split(" ");
alert(array[0]);
arrayOfFirstNames.push('"' + array[0] + '"');
arrayOfLastNames.push('"' + array[1] + '"');
arrayOfPhoneNumbers.push('"' + array[2] + '"');
}
alert(arrayOfFirstNames[0]);
I don't have your HTML so I havn't tested this code, but it should do the job if all you want is 3 arrays of first names, last names and phone numbers that you can access using square bracket notation.

Cannot read properly "split"

I have this piece of code in javascript:
var i = 0;
var j = 0;
while (allTextLines.length) {
var line = allTextLines.shift().split('"');
var temp = line[1].split('');
if (i == 0) {
alert(temp);
i++;
}
var x = (temp[0] + temp[1]+ temp[2] + temp[3] + "-" + temp[4] + temp[5] + temp[6]);
var y = line[3];
var z = line[5];
var g = line[7];
lines[j] = (x + ", " + z + ", " + g);
j++;
}
And it's happening something really weird. When i==0, it alerts temp and its split. After that I'm getting:
Uncaught TypeError: Cannot read property 'split' of undefined
If I remove the if, I will have this error right on the start. But if I do something like this:
var line = allTextLines.shift().split('"');
var temp = line[1].split('');
alert(temp);
var x = (temp[0] + temp[1]+ temp[2] + temp[3] + "-" + temp[4] + temp[5] + temp[6]);
It has no problem splitting (also the alert shows that it has been correctly split). The only problem is that I will have to click "ok" 5600 times. I don't understand what the hell is going on and why I am having this error.
I'm splitting a CSV file with lines like this:
35105,201401,503781827,"8400258","Faro","Lagoa (Algarve)","Portugal"
and I'm trying to add an '-' in here: "8400258", so it becomes "8400-258"
var line = allTextLines.shift().split('"');
var temp = line[1].split('');
Won't that fail when allTextLines only has one element in it, since the array is zero-based? I think you'd need to change the line[x] param to 0:
var temp = line[0].split('');
Might I suggest a different approach?
var line = allTextLines.shift().split(',');
var x = line[3].replace(/^"(\d{4})(\d{3})"$/, "$1-$2");
var y = line[4];
var z = line[5];
var g = line[6];
If you can trust that the format of the data is always the same, it's much less complex to do a pattern based replace than splitting a string like an array and re-assembling it.

Categories