How do you capitalize a whole word in javascript? [duplicate] - capitalization

This question already has answers here:
Swap Case on javascript
(11 answers)
Closed 3 years ago.
can someone tell me how to capitalize my whole name from Jason Sims to JASON SIMS please!
['Name:', 'Jason Sims'],
['Career: Welder'],
['Description: In transition to becoming a developer!'],
];```

This way
var name = "Whatever";
console.log(name.toUpperCase());

You can use map for it:
const data = [
['Name:', 'Jason Sims'],
['Career: Welder'],
['Description: In transition to becoming a developer!'],
];
const altData = data.map(pair => {
return pair[0] === 'Name:' ? [pair[0], pair[1].toUpperCase()] : pair;
});
console.log(altData)

Related

DataLayer - list of all skus purchased [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 5 years ago.
Hi please how would you set the variable to get the list of all skus purchased which are in an array in the data layer? For example:
transactionProducts:[
{sku: “1234”, name: “product 1”, price: “2.99”, quantity: “1”}
{sku: “5678”, name: “product 2”, price: “5.99”, quantity: “1”}
{sku: “9012”, name: “product 3”, price: “8.49”, quantity: “2”}
]
From the above array, I would want to get each sku value and have it return together as the value of the variable. So something like '1234', '5678', '9012'
Because now I make variable transactionProducts.0.sku but its only one.
Is there any way to do that?
Thank you
If you want to use a custom JS in GTM:
function () {
sku = []
for (i=0; i< transactionProducts.length; i++) {
sku.push(transactionProducts.i.sku);
}
return sku;
}

push to an array in a "then" clause but it's empty [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
I'm adding some keys from a firebase database to an array, but it remains empty. The code is the following:
ref.once("value")
.then(function(snapshot) {
snapshot.forEach(tag =>{
tags1.push(tag.key);
})
});
I checked for the value of tag.key by printing it on the console inside the forEach, and all the keys are printed correctly.
Then, I've tried to perform a forEach on the tags1 array, but it doesn't enter the for. I've printed the length of tags1 and it is 0.
I've declared tags1 this way:
let tags1 = [];
I've also tried to declare it as Array<any>, but it keeps staying empty.
Let's go over the sequence in which things happen:
const tags1 = []; // 1 -- EMPTY
ref.once("value") // 2 -- EMPTY
.then(function(snapshot) {
snapshot.forEach(tag => {
tags1.push(tag.key); // 4 -- GETS NEW VALUES
});
console.log(tags1); // 5 -- HAS ALL VALUES
});
console.log(tags1); // 3 -- STILL EMPTY!

$.grep on JSON data in multiple array.fields using wildcards?

First off I have looked through similar looking questions but have not found the exact problem asked or answered, so here goes :
I have a JSON Object which consists of about 900+ posts. Looking like this:
var JsonData = [{"rowNumber":563663,"hasWarning":true,"isInvoiceAccount":true,"phone":"","name":"Romerike AS","address1":"Co/Skanning","address2":"PB 52","attention":"","mobile":"","email":"fakt#bos.no","fax":"","zipCity":"N-1471 Askim","invoiceAccount":"","notes":null,"account":"3","country":"NORGE","salesRep":"4","countryCode":"no"},{"rowNumber":563674,"hasWarning":false,"isInvoiceAccount":true,"phone":"","name":"LILLEHAMMER","address1":"POSTBOKS 110","address2":"","attention":"","mobile":"","email":"","fax":"","zipCity":"N-2605 LILLEHAMMER","invoiceAccount":"","notes":null,"account":"14","country":"NORGE","salesRep":"4","countryCode":"no"},{"rowNumber":563676,"hasWarning":true,"isInvoiceAccount":true,"phone":"63929788","name":"Askim Bil AS","address1":"Postboks 82","address2":"","attention":"","mobile":"","email":"karosseri#nyg.no","fax":"","zipCity":"N-2051 Askim","invoiceAccount":"","notes":null,"account":"16","country":"NORGE","salesRep":"4","countryCode":"no"},{"rowNumber":563686,"hasWarning":false,"isInvoiceAccount":true,"phone":"69826060","name":"KAROSSERI A/S","address1":"POSTBOKS 165","address2":"","attention":"","mobile":"","email":"tkar#online.no","fax":"","zipCity":"N-1860 TRØGSTAD","invoiceAccount":"","notes":null,"account":"26","country":"NORGE","salesRep":"4","countryCode":"no"},{"rowNumber":563690,"hasWarning":false,"isInvoiceAccount":true,"phone":"","name":"AUTOSERVICE A/S","address1":"POSTBOKS 15","address2":"","attention":"","mobile":"","email":"","fax":"","zipCity":"N-2851 LENA","invoiceAccount":"","notes":null,"account":"30","country":"NORGE","salesRep":"4","countryCode":"no"},{"rowNumber":563691,"hasWarning":false,"isInvoiceAccount":false,"phone":"","name":"ØYHUS A/S","address1":"POSTBOKS 321","address2":"","attention":"John Doe","mobile":"","email":"","fax":"","zipCity":"N-2817 GJØVIK","invoiceAccount":"","notes":null,"account":"31","country":"NORGE","salesRep":"4","countryCode":"no"}];
I want to filter these data before I read them into a table using $.grep.
The JSON data have been loaded as an object.
In the HTML page I have a textfield named "filter".
The following code works, but only when I search for an exact match:
var JsonFiltered = $.grep(JsonData, function (element, index) {
return element.zipCity == $('#filter').val();
});
$.each( JsonFiltered, function ( index, value ) {
// sorting through the array adding values to a table
[...]
});
Problem 1:
I want to use Wildcards when filtering.
I read something about using regexp but I haven't found any viable examples.
Problem 2:
I want to be able to filter more than one column.
Example: filtering the word "Askim" in both element.name and element.zipCity
So I figured out the solutions myself...
Using Wildcards:
var search_term = $('#filter').val();
var search = new RegExp(search_term, "i");
var JsonFiltered = $.grep(JsonTest, function (element, index) {
var zipC = search.test(element.zipCity)
var names = search.test(element.name)
return zipC + names ;
The solution was to use "new RegExp" with the filter "i" setting.
then I took two search.tests combined them in the return command and... presto
Hope this helps anyone else.

Checking number of times CSS classes called. [duplicate]

This question already has answers here:
How to identify unused CSS definitions from multiple CSS files in a project
(3 answers)
Closed 9 years ago.
I was thinking of writing a script which would tell me:
How often each CSS class defined in my .css file is used in my code
Redundant CSS classes - classes never used
CSS classes hat are referenced that don't exist.
But I just want to make sure something like this doesn't exist already? Does it?
Thanks
Just for fun, I wrote one.
try it
First we need to find our style sheet. In an actual script, this would be written better, but this works on jsFiddle.
var styles = document.head.getElementsByTagName('style');
var css = styles[styles.length - 1].innerHTML;
Then remove comments, and the bodies of each selector (i.e. the stuff between the brackets). This is done because there could be a .com in a background-image property, or any number of other problems. We assume there isn't a } in a literal string, so that would cause problems.
var clean = css.replace(/\/\*.*?\*\//g, '').replace(/\{[^}]*\}/g, ',');
We can find classes with regular expressions, and then count how many of them occur.
var re_class = /\.(\w+)/g;
var cssClasses = {}, match, c;
while (match = re_class.exec(clean)) {
c = match[1];
cssClasses[c] = cssClasses[c] + 1 || 1;
}
I used jsprint for displaying our findings. This shows how many times each class is mentioned in our CSS.
jsprint("css classes used", cssClasses);
Thanks to Google and this answer we can find all elements in the body, and loop through them. By default, we assume no classes were used in our HTML, and all classes used were defined.
var elements = document.body.getElementsByTagName("*");
var neverUsed = Object.keys(cssClasses);
var neverDefined = [];
var htmlClasses = {};
We get each elements class, and split it on the spaces.
for (var i=0; i<elements.length; i++) {
var e = elements[i];
var classes = (e.className || "").split(" ");
This is a three dimensional loop, but it works nicely.
for (var j=0; j<classes.length; j++) {
for (var k=0; k<neverUsed.length; k++) {
We thought classes[j] was never used, but we found a use of it. Remove it from the array.
if (neverUsed[k] === classes[j]) {
neverUsed.splice(k, 1);
}
}
It looks like we found a class that doesn't appear in our CSS. We just need to make sure it's not an empty string, and then push it onto our array.
if (classes[j].length && cssClasses[classes[j]] == null) {
neverDefined.push(classes[j]);
}
Also count the number of times each class is used in HTML.
if (classes[j].length) {
htmlClasses[classes[j]] = htmlClasses[classes[j]] + 1 || 1;
}
}
}
Then display our results.
jsprint("html class usage", htmlClasses);
jsprint("never used in HTML", neverUsed);
jsprint("never defined in CSS", neverDefined);

How can we filter text value from string [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I need single different string value from my resulted string.
My string variable return value like:
string result = "1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1"; // from here I'll need 2
or string result = "1,1,1,1,1,1,1,1,4,1,1,1,1,1,1,1"; // from here I'll need 4
or string result = "1,1,1,1,1,1,1,1,1,1,1,1,1,1,3,1"; // from here I'll need 3
var finalValue = result.Replace(",","").Replace("1","");
Just replace coma(,) and 1 with blank('').
string single_value = result.Replace(",","").Replace("1","");
If you are trying to get the value that never appears more than once try the below code
string result = "1,1,1,1,1,1,1,1,1,1,1,1,1,1,3,1";
var UniqueElement = result.GroupBy(x => x)
.Where(g => g.Count() == 1)
.Select(g => g.Key)
.ToList();
Console.WriteLine(UniqueElement[0]);
This will work even if you have different values other than '1'
Example: ",1,1,1,1,2,2,2,4,1,1,1,1,1,1,1"
You can use Enumerable.Max, Max() will return maximum char value from string. If your string consists of commas and numbers only, that will be the maximum number, result is char of course:
result.Max();
If you only want the value that occurs once than this will work.
var value = result.Split(',')
.GroupBy(v => v)
.Where(g => g.Count() == 1)
.First()
.Key;

Resources