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;
}
Related
I have relation data which is in record form:
Record : { Id: 40, Material: test, Size: test, Description: test, Quantity: test, Spec: test, Price:
test, Delivery: test, Quotes_fk: 21},
Record : { Id: 43, Material: test 2, Size: test 2, Description:
test 2, Quantity: test2, Spec: test2, Price: test2, Delivery: test2, Quotes_fk: 21}
I need to present this as a table on a pdf with the headings Id,Material,Size, Description, Quantity, Spec, Price, Delivery and as I am new to coding can’t work it out. I have managed to use Regex format to make it presentable, but it needs to be in table format. So far I can change the data to a string them replace some of the commas with line breaks.
var quotes = questionnaire.QuoteItems;
var quotes2 = quotes.toString();
var quotes3 = quotes2.replace(/{/g , "<br>");
Managed to work through each item in the related record and display using the following code:
for (var i=0; i < questionnaire.QuoteItems.length; i++) {
rows.push([questionnaire.QuoteItems[i].Material, questionnaire.QuoteItems[i].Size, questionnaire.QuoteItems[i].Description, questionnaire.QuoteItems[i].Quantity, questionnaire.QuoteItems[i].Spec, questionnaire.QuoteItems[i].Price, questionnaire.QuoteItems[i].Delivery]);
}
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)
$Keys and $Values are useful, but they don't help if I want to create this kind of union type:
type Screen = 'screen1' | 'screen2' | 'screen3';
const transitions = [
{name: 'from1to2', from: 'screen1', to: 'screen2'},
{name: 'from2to3', from: 'screen2', to: 'screen3'},
{name: 'from3to2', from: 'screen3', to: 'screen2'},
{name: 'from2to1', from: 'screen2', to: 'screen1'},
];
// DRY here! But how?
type Transition = 'from1to2' | 'from2to3' | 'from3to2' | 'from2to1';
Thanks for any advice!
Looks like the best solution so far is a workaround like this:
type Screen = 'screen1' | 'screen2' | 'screen3';
const transitionsConfig = {
from1to2: {from: 'screen1', to: 'screen2'},
from2to3: {from: 'screen2', to: 'screen3'},
from3to2: {from: 'screen3', to: 'screen2'},
from2to1: {from: 'screen2', to: 'screen1'},
};
const transitions = Object.keys(transitionsConfig).map(k => ({
name: k,
...transitionsConfig[k],
}));
type Transition = $Keys<typeof transitionsConfig>;
I don't think it's possible to do this. The values in your transitions array are runtime values, while types are a compile-time abstraction. In other words the compiler can't look inside your array because it essentially doesn't exist (or at least, doesn't exist as a thing with a value yet). You probably will have to define your transition types individually and create a union type between them.
Cool idea though. :)
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!
I am using auto schema to define an array field. I need to find documents where multiple specific values are contained in that array. I know I can use the $in: operator while $in: can only match either one of the value in the first array against the second array while I would need to match any record that have all value in the first array. How I can achieve this?
Schema Definition
Demands = new Mongo.Collection("demands");
var demandschema = new SimpleSchema({
ability: {type:array},
language: {type: array}});
Demands.attachSchema(demandschema);
Contents Definition
DemandsSet=[
{ability: ["laser eye", "rocky skin", "fly"], language: ["english", "latin", "hindu"]},
{ability: ["sky-high jump", "rocky skin", "fly"], language: ["english", "latin", "japanese"]},
{ability: ["rocky skin", "sky-high jump"], language: ["english", "latin", "russian"]}
];
Target Set
var TargetAbility = ["rocky skin", "fly"];
var TargetLanguage = ["english", "hindu"];
When I do a $in operation
Demands.find({ $and: [
{ ability: { $in: TargetAbility }},
{ language: { $in: TargetLanguage }}
]}).fetch();
I will return me with all records, while it is not correct, how can I perform such a find operation?
$in: is not going to work for you because it looks for any match when comparing two arrays, not that all elements of one array must be present in the other.
You can write complete javascript functions to execute the required comparisons inside the mongodb query. See $where:
For example:
Demands.find({$where:
"this.ability.indexOf(TargetAbility[0]) > -1 &&
this.ability.indexOf(TargetAbility[1]) > -1 &&
this.language.indexOf(TargetLanguage[0]) > -1 &&
this.language.indexOf(TargetLanguage[1]) > -1" });
If your candidates have other than 2 entries each then you can write a more general form of this of course.
Note that Meteor apparently does not support the function() form of $where: but that restriction may be dated.
Also note that $where: cannot take advantage of indexes so performance may not be suitable for large collections.