Reputation: 419
As I asked in a previous question and I got a good and working answer :
How to store the URL's and Titles of a list of posts under same label into a String array in Blogger
Now I want to know how to make a variable point or get the index number of the particular url.
Suppose I use var cURL="<data:post.url>"
to store the URL of the post I am currently browsing on the blog into cURL
variable.
Supposedly the blog post I have currently opened happens to be:
SITEURL
And the variable cURL
stores this URL. I want a code to search and match this URL in a array which I have stored a number of URL's
Now if the value of cURL
is as already above
If the array is var URLArray
and its data with index happens to be:
As highlighted in bold above, the URL in URLArray[2]
matches the URL stored in cURL
.
Now I want this Index number 2
stored in a variable.
Since blogger doesn't allow normal java for
loops, I don't know how to do it.
Upvotes: 1
Views: 624
Reputation: 5651
You can use the JavaScript method findIndex() for this purpose. The code for same will be of the form
<script>
var URLArray = <b:eval expr='data:posts map (post => post.url)'/>;
var cURL = "<data:post.url/>";
function IndexFinder(element,index) {
return element == cURL
}
var storeIndex = URLArray.findIndex(IndexFinder);
</script>
Upvotes: 1
Reputation:
To get index number through loop use index attribute as in the following example :
<b:loop index='i' values='data:posts' var='post'>
<data:i/> : <data:post.url/>
</b:loop>
Notes :
i
can be any name you choose.Javascript version :
<script type="text/javascript">
var URLArray = [];
<b:loop index='i' values='data:posts' var='post'>
URLArray[<data:i/>] = <data:post.url/>;
</b:loop>
</script>
Upvotes: 1