1

I am trying to combine an array of strings together into a one lined variable

So for example I want the end result to render out like:

"tag[]=sku_helloworld&tag[]=sku_bridesdark&tag[]=stuk_home"

This is what I have at the moment and I'm unsure on how to combine it alltogether?

var productSku = $('.social-module').data('magento-sku'),
  str = productSku,
  skuList = str.split(',');

for (var i = 0; i < skuList.length; i++) {
  console.log("tag[]=" + skuList[i] + "&");
}

https://jsfiddle.net/tvdberf7/

0

7 Answers 7

1

You can simplify it a little bit like this:

var productSku = $('.social-module').data('magento-sku'),
  str = productSku,
  skuList = 'tag[]=' + str.split(',').join('&tag[]=');

See the updated JSFiddle.

Sign up to request clarification or add additional context in comments.

Comments

1

You could use Array#map() and Array#join()

var skuList = ['sku_helloworld', 'sku_bridesdark', 'stuk_home'],
    result = skuList.map(function (a) {
        return 'tag[]=' + a;
    }).join('&');

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

ES6

var skuList = ['sku_helloworld', 'sku_bridesdark', 'stuk_home'],
    result = skuList.map(a => 'tag[]=' + a).join('&');

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

Comments

0

Put it in one string, then print it.

var productSku = $('.social-module').data('magento-sku'),
  str = productSku,
  skuList = str.split(',');
  var result = "";
for (var i = 0; i < skuList.length; i++) {
  result += "tag[]=" + skuList[i] + "&";
  console.log(result);
}

JSfiddle

Comments

0

You could use reduce (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce)

skuList.reduce(function(a, b) { return a + "&tag[]=" + b }, "").substring(1)

The substring takes care of removing the first &

Comments

0

You should be able to use the Array.join() method to handle this, however you would need to explicitly add on your first delimiter :

// Array.join will build a string using your delimiter '&tag[]=' between
// each of the items in the array
var result = 'tag[]=' + skuList.join('&tag[]=');

You can see an example of this in action here.

Comments

0

You can generate the list like this:

skuList = str.split(',').map(function(item) {return "tag[]=" + item}).join('&');
console.log(skuList);

Comments

0

Here's a one-liner with no looping:

'tag[]=' + productSku.split(',').join('&tag[]=')

var productSku = $('.social-module').data('magento-sku');

alert('tag[]=' + productSku.split(',').join('&tag[]='));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div data-magento-sku="sku_helloworld,sku_bridesdark,stuk_home" class="social-module"></div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.