Is it possible to declare a variable differently for each iteration? Here is the general idea:
var userIds = [9110252, 55829847, 145189041]
for(u = 0; u < userIds.length; u++){
console.log(userIds[u]);
var user+userIds[u] = userIds[u];
}
It's not possible. But you also don't need that:
You won't be generating dynamic variable names, but you can have a different variable in each iteration of the for loop:
var userIds = [9110252, 55829847, 145189041]
for(u = 0; u < userIds.length; u++){
console.log(userIds[u]);
var user = userIds[u];
}
On the first iteration, user will hold 9110252, on the second a new value is set to variable user: 55829847 and so forth.
But in this case, as #adeneo mentioned: You already have: userIds[u] to refer to the value.
We have arrays for that .Why do u need to have different name of variable when one array variable can do it for u and also it makes code easy to manage.
Reading through the comments on the question and wanting to store it inside local storage. I would do this:
var userIds = [9110252, 55829847, 145189041];
for (var i = 0; i < userIds.length; i++) {
var userId = 'user' + userIds[i];
window.localStorage.setItem(userId, userIds[i]);
}
I would recommend however to reconsider this type of storage, because you're now storing redundant data. It's only distinguished with the word "user" in front of it.
User #Abhishek Panjabi also mentioned that this is the reason why we have arrays. He is correct in saying this.
Credits to user #adeno for his comment.
Related
I thought of somethinh like this:
var userGames = {};
for (i=0; i< client.getListOfOnlineUsers(); i++) {
var key = client.getListOfOnlineUsers()[i];
userGames.key = client.getListOfOnlineUsers()[i].presence.game;
}
Is this the right way to go?
I haven't used discordjs, so correct me if I got anything wrong. As I understand it, getListOfOnlineUsers returns the array of users and user.presence.game will give you the game. If so, you have the correct idea, just a minor correction on line 4:
var userGames = {};
for (i=0; i< client.getListOfOnlineUsers(); i++) {
var key = client.getListOfOnlineUsers()[i];
userGames[key] = client.getListOfOnlineUsers()[i].presence.game;
}
This should work for you. The [key] is used here because key is a variable and the real value of the dictionary key is computed at run-time.
Also, you should probably avoid calling the same function over and over when it returns the same result. Save the data in a variable. Maybe, use forEach and let too.
let userGames = {};
const userList = client.getListOfOnlineUsers();
userList.forEach(u => userGames[u] = u.presence.game);
Looks a lot cleaner imo.
I'm using the DuracellTomi datalayer plugin to push cart data from woocommerce to a GTM model to handle some tracking.
The DuracellTomi plugin pushes content to the transactionProducts[] array in the following format:
transactionProducts: Array[1]
0 : Object
category:""
currency:"USD"
id:8
name:"Test"
price:100
quantity:"1"
sku:8
I'd like to loop through this array and unstack it into three separate arrays, pricelist, skulist, and quantitylist. Currently I anticipate doing so as some variation on
//Get Product Information
if(stack = {{transactionProducts}}){
for(i = 0; i < stack.length; i++) {
if(stack.i.sku){
skulisttemp.i = stack.i.sku;
}
if(stack.i.price){
pricelisttemp.i = stack.i.price;
}
if(stack.i.sku){
quantitylisttemp.i = stack.i.quantity;
}
}
{{skulist}} = skulisttemp;
{{pricelist}} = pricelisttemp;
{{quantitylist}} = quantitylisttemp;
}
Obviously this is not going to work because of how the tag referencing is set up, but I'm wondering if anyone has dealt with this and knows what the best way to index through these arrays might be. (For those who don't know, the square bracket array call doesn't work with GTM variables and instead the . format is used instead.)
You would need to create 3 variable type custom javascript function that picks your required value from dataLayer and returns it in an array.
Something like
function(){
var products = {{transactionProducts}};
var skuArray = [];
for(i = 0; i < products.length; i++) {
if(products[i].sku){
skuArray.push(products[i].sku)
}
}
return skuArray
}
hope this helped you :)
This doesn't work but I can't see why it wouldn't? any help people? :)
params = qs.split("=", 2),
id = params[1];
if(id.indexOf("?") != -1){
id = id.split("?", 1);
}
basically I want to change the value of 'ID' if the IF statement is true, if not.. it skips it and the value Id remains the default.
Thanks
The result of id = id.split("?", 1) is an array (of at most 1 item), but I think you want id to be a string. That would explain why id is not a string like you want.
I agree with the other comments. Please show us the URL string you want to parse and tell us which piece you're trying to get. Usually, you look for ? first, separate after that and then divide up various key=value sections.
If you had a URL like this:
http://www.example.com?foo=bar
Here's a simple function that gets you all the query parameters into an object:
function getParms(url) {
var sections, key, pieces = url.split("?");
var results = {};
if (pieces.length > 1) {
sections = pieces[1].split("&");
for (var i = 0; i < sections.length; i++) {
key = sections[i].split("=");
results[key[0]] = key[1];
}
}
return(results);
}
Working demo: http://jsfiddle.net/jfriend00/kNG3u/
I am trying to break a javascript object in to small array so that I can easily access the innerlevel data whenever I needed.
I have used recursive function to access all nodes inside json, using the program
http://jsfiddle.net/SvMUN/1/
What I am trying to do here is that I want to store these in to a separate array so that I cn access it like
newArray.Microsoft= MSFT, Microsoft;
newArray.Intel Corp=(INTC, Fortune 500);
newArray.Japan=Japan
newArray.Bernanke=Bernanke;
Depth of each array are different, so the ones with single level can use the same name like I ve shown in the example Bernanke. Is it possible to do it this way?
No, you reduce the Facets to a string named html - but you want an object.
function generateList(facets) {
var map = {};
(function recurse(arr) {
var join = [];
for (var i=0; i<arr.length; i++) {
var current = arr[i].term; // every object must have one!
current = current.replace(/ /g, "_");
join.push(current); // only on lowest level?
if (current in arr[i])
map[current] = recurse(arr[i][current]);
}
return join;
})(facets)
return map;
}
Demo on jsfiddle.net
To get the one-level-data, you could just add this else-statement after the if:
else
map[current] = [ current ]; // create Array manually
Altough I don't think the result (demo) makes much sense then.
Hey guys I've got 2 dim array and a hash!
Array's second row values and hash keys are set identical!
What I want is to address each hash key using array's row values and change them to array's current column index
Preview example:
{.....,'_11':val, '_12':value, .....}
arr[1][i]='_12'. use this value to address the the unique hash hey and change that key to i. key=i
Is this the right way?
var keyName;
for(var i=0; i<theLength; i++){
keyName = arr[1][i];
hash.keyName=i;
}
10x for your kind help ,BR
Maybe what you want is this:
var keyName;
for(var i=0; i<theLength; i++) {
keyName = arr[1][i];
hash[keyName] = i;
}
Using hash.keyName will always reference a key called keyName, not the key with that variable name.
Since you don't really need the intermediate variable, you can do this:
for(var i=0; i<theLength; i++) {
hash[arr[1][i]] = i;
}
Not sure I follow what you're asking for the rest, but
hash.keyName=i;
should be:
hash[keyName]=i;