问题
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:
http://rawmangaread.blogspot.in/2017/03/himekishi-ga-classmate-chapter-3-raw.html
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:
URLArray[0]=http://rawmangaread.blogspot.in/2017/03/himekishi-ga-classmate-chapter-1-raw.html
URLArray[1]=http://rawmangaread.blogspot.in/2017/03/himekishi-ga-classmate-chapter-2-raw.html
URLArray[2]=http://rawmangaread.blogspot.in/2017/03/himekishi-ga-classmate-chapter-3-raw.html
URLArray[3]=http://rawmangaread.blogspot.in/2017/03/himekishi-ga-classmate-chapter-4-raw.html
URLArray[4]=http://rawmangaread.blogspot.in/2017/03/himekishi-ga-classmate-chapter-5-raw.html
URLArray[5]=http://rawmangaread.blogspot.in/2017/03/himekishi-ga-classmate-chapter-6-raw.html
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.
回答1:
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>
回答2:
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.- Loop index starts from zero.
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>
来源:https://stackoverflow.com/questions/43154669/how-to-match-and-point-to-a-particular-data-in-array-in-blogger