0

I have been working on implementing a review system into my website and am stuck at this point:

I take all the product SKUs which are only accessible from the Cart page like such:

<span class="ProdSkus">SKU1</span>
<span class="ProdSkus">SKU2</span>
<span class="ProdSkus">SKU3</span>

I'm combining these into an array with this:

var combinedText = $('.ProdSkus').text();

When I log it I get this result:

SKU1SKU2SKU3

I need to format it to be as such:

["SKU1", "SKU2", ...]

Any help is appreciated!

This has to be done from the cart page, stored locally, and then used after the customer has made the purchase. I have been doing this like such:

localStorage.setItem("SKUS", combinedText);
1
  • You can't store an array in localStorage, you need to convert it to JSON. Commented Feb 22, 2017 at 19:17

4 Answers 4

2

Use .map():

var combinedText = $('.ProdSkus').map(function() {
    return $(this).text();
}).get();

.map() returns a jQuery collection of the return values (strings in this case), and .get() converts it to an array.

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

1 Comment

Clarification: The JQuery version of map() returns a JQuery object containing whatever you tell it to contain. In this case, the text of the elments (strings).
0

You can iterate the set of elements and add each value to an array:

var combinedText = [];  // This is where the results will go

// Loop over the matched elements
$('.ProdSkus').each(function(){
  // Add the text of each span to the array
  combinedText.push(this.textContent);
});

// Test the result
console.log(combinedText);

// And, to store the values in localStorage,
// you'd need to convert the array into a string
// This will throw an error here in Stack Overflow because
// their snippet environment doesn't allow it, but the code is correct:
localStorage.setItem("combinedText", JSON.stringify(combinedText));

// Then, when/where you want the data back out of localStorage, you'd write
var gottenData = localStorage.getItem("combinedText");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="ProdSkus">SKU1</span>
<span class="ProdSkus">SKU2</span>
<span class="ProdSkus">SKU3</span>

4 Comments

This works great! It's formatted exactly as needed. Thank you!
@Matt You're welcome. Don't forget to up vote all the helpful answers and mark one (maybe this one?) as your answer.
Apparently I cannot do that as of yet, ha. Need 15 rep, but I have upvoted these! Now my next task is to convert it to be stored in local storage, I think stringify does this?
@Matt All you'd need to do is localStorage.setItem("combinedText", JSON.stringify(combinedText));
0

Pure JS solution (no jQuery).

var elems = document.getElementsByClassName('ProdSkus');
var arr = [];
Array.from(elems).forEach(v => arr.push(v.innerHTML));

console.log(arr);
<span class="ProdSkus">SKU1</span>
<span class="ProdSkus">SKU2</span>
<span class="ProdSkus">SKU3</span>

Comments

0

Try using each:

var combinedText = [];
$('.ProdSkus').each(function(k,v) {
    combinedText.push(v.text());
});
console.log(combinedText);

1 Comment

v is the element, not its text.

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.