我有一个字符串数组,其中包含以字母开头的元素,然后是冒号,然后是一些随机文本。例如:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar navbar-default" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-1"> <span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button> <a class="navbar-brand" href="#">Project</a>
</div>
<div class="collapse navbar-collapse" id="navbar-1">
<ul class="nav navbar-nav">
<li><a href="http://2015.igem.org/Team:Michigan/Project">Home <span class="sr-only">(current)</span></a>
</li>
<li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Project <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="http://2015.igem.org/Team:Michigan/Design">Design</a>
</li>
<li><a href="http://2015.igem.org/Team:Michigan/Experiments">Experiments & Protocols</a>
</li>
<li><a href="http://2015.igem.org/Team:Michigan/Results">Results & Discussion</a>
</li>
<li><a href="http://2015.igem.org/Team:Michigan/Collaborations">Collaborations</a>
</li>
<li><a href="http://2015.igem.org/Team:Michigan/Parts">Parts</a>
</li>
</ul>
</li>
<li><a href="http://2015.igem.org/Team:Michigan/Notebook">Notebook</a>
</li>
<li><a href="http://2015.igem.org/Team:Michigan/Modeling">Modeling</a>
</li>
<li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Human Practices<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="http://2015.igem.org/Team:Michigan/Background">Background</a>
</li>
<li><a href="http://2015.igem.org/Team:Michigan/Diseases">Diseases and Applications</a>
</li>
<li><a href="http://2015.igem.org/Team:Michigan/Current">Current Detection Methods</a>
</li>
<li class="dropdown dropdown-submenu"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Informed Design <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="http://2015.igem.org/Team:Michigan/Survey">Survey</a>
</li>
<li><a href="http://2015.igem.org/Team:Michigan/Chemicals">Biotech/Pharmaceutical Industry</a>
</li>
<li><a href="http://2015.igem.org/Team:Michigan/Food">Food Science Industry</a>
</li>
<li><a href="http://2015.igem.org/Team:Michigan/Medical">Medical Practice: Point of Care Testing</a>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="http://2015.igem.org/Team:Michigan/Safety">Safety</a>
</li>
</ul>
</div>
</nav>
我想只保留以a = [
"a:aushdad",
"b:ofigjhld",
"g:dkfgjdfg",
"b:kjfdgkdfj"
]
开头的第一个元素,并且对于可能存在的任何其他重复项都是如此。
最终结果应为:
b:
如何实现这一目标?如果您需要更多信息,请与我们联系。
答案 0 :(得分:2)
您可以使用Array.reduce:
var a = [
"a:aushdad",
"b:ofigjhld",
"g:dkfgjdfg",
"b:kjfdgkdfj"
]
var letters = [];
var result = a.reduce(function(prev,current){
if(letters.indexOf(current[0]) === -1) {
letters.push(current[0]);
prev.push(current);
}
return prev;
}, []);
console.log(result); // ["a:aushdad", "b:ofigjhld", "g:dkfgjdfg"]
答案 1 :(得分:1)
您可以使用数组中的filter。
//store already used 'key'
var map = {};
//regexp used to get the interesting part
var regexp = /^.*?:/;
//function used for the filter, true add the value to the result array.
function myFilter(value){
//get the key
var key = regexp.exec(value)[0];
//key not null and not used ?
if(key && !map[key]){
map[key] = true;
return true;
}
else{
return false;
}
}
var result = myArray.filter(myFilter);