Ad
Return Array Without Quotes
The scenario
I want to push product gtins to Google Merchant Center.
What I'm getting
"products": ['{"gtin":"5704378978422"}', '{"gtin":"5704378978057"}']
What I want to get
Without single quotes for each array member in the format:
"products": [{"gtin":"GTIN1"}, {"gtin":"GTIN2"}]
As described in step 3 here: https://support.google.com/merchants/answer/7519329
My Google Tag Manager setup
- I have created a Data Layer Variable {{DLV - ecommerce.purchase.products}} from ecommerce.purchase.products
- I have created a Custom Javascript variable with the following code:
Code:
function() {
var products = {{DLV - ecommerce.purchase.products}};
return products.reduce(function(arr, prod) {
return arr.concat("{" + '"' + "gtin" + '"' + ":" + '"' + prod.gtin + '"' + "}" ); }, []);
}
What would be the best way to do this
I'm new to JavaScript, and would appreciate any comments and suggestions
Ad
Answer
By using the concat
function you are creating a string concatenation, not an object.
You should probably use map
instead of reduce
. Like this:
function() {
var products = {{DLV - ecommerce.purchase.products}};
return products.map(function(prod) {
return {"gtin": prod.gtin};
});
}
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad