Reputation: 50
I want to add the index position of each element at the end of that element. eg: <Category>;<Action>;<Label>;<Position>
at the moment I only have <Category>;<Action>;<Label>
I dont know how to loop through them again to get the position and append that. Below is some example code. Not sure if this makes any sense. Thank you for any help.
var newArr = [];
var string = "~A~, ~B~, ~C~";
string = string.split(",");
$.each(string, function(index, element) {
console.log("Log 1: " + element);
element = element.replace(/\~/g, "").replace(" ", "");
console.log("Log 2: " + element)
newArr.push(element);
})
newArr = newArr.join(";");
console.log(newArr)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Upvotes: 0
Views: 28
Reputation: 40
You could have a variable outside the loop let i = 0; then
newArr.push(i++);
newArr = bannerDataArr.join(";");
Hope this helps. The code is a little confusing.
Upvotes: 1