[英]how to get the value of selected index of array
我在Java腳本中有數組,我希望它可以給出該索引的值並以html的形式顯示給div數據,現在它顯示數組的索引不是值,我想要的值我給出了jsfiddle鏈接
<div id="data"></div>
and here is the script
var ad = ['a', 'b', 'c', 'a', 'd', 'e', 'a'];
var con=document.getElementById('data');
var inds = $.map(ad, function(v, i) {
return v == 'b' ? i : null;
});
alert(inds); // 0,3,
var avr1=inds;
con.innerHTML=avr1;
你期望“ b”嗎? 更改此行:
var avr1=inds;
至
var avr1=ad[inds];
要獲得索引inds
處的ad
元素-> ad[1] = 'b'
編輯:您可以使用以下命令獲取輸出“ aaa”,它比您的方法要簡單一些,它會計算數組中“ a”的數量,然后根據該數量生成一個字符串。
var con = document.getElementById('data');
var char = 'a';
var str = '';
var ad = ['a', 'b', 'c', 'a', 'd', 'e', 'a'];
var count = ad.filter(function (v) {
return v == char;
}).length;
for (var i = 0; i < count; i++)
str += char;
alert(str);
con.innerHTML = str;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.