如何从javascript中的字符串数组中获取非重复值 [英] How to get the non recurring value from a String Array in javascript
本文介绍了如何从javascript中的字符串数组中获取非重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在javascript中从字符串数组中获取非重复[不重复]值
例如:
如果字符串数组
A = [850,851,850]
那么,我需要o / p为:
851
解决方案
好的,有一种更简单的方法,使用indexof
http://www.w3schools.com/jsref/jsref_indexof_array.asp [ ^ ]
var i = 0;
for(i; i< arr.length; ++ i)
{
if(arr.indexOf(arr [i]))== arr.lastIndexOf( arr [i]))
{
//这意味着它只在那里一次。
}
}
试试这个..
var A = [ 850 , 851 , 850 ];
var output = new 数组();
for ( var i = 0 ; i< A.length; i ++){
var current = A [i];
var flag = 0 ;
for ( var j = 0 ; j< A.length; j ++){
if (current == A [j])
flag ++;
}
if (flag == 1 )
输出。推(电流);
}
alert(输出);
对我而言,这看起来是一系列数字。
无论如何,你可以迭代数组的项目,使用IndexOf
方法检查它是否有重复,在第一次非重复检测时停止迭代
How to get the non recurring[not duplicate] value from a String Array in javascript
For eg:
if the String Array
A=[850,851,850]
then,i need the o/p as:
851
解决方案
OK, there is an easier way, using indexof
http://www.w3schools.com/jsref/jsref_indexof_array.asp[^]
var i = 0; for(i; i < arr.length;++i) { if (arr.indexOf(arr[i])) == arr.lastIndexOf(arr[i])) { // This means it's only in there once. } }
try this..
var A = [850, 851, 850]; var output = new Array(); for (var i = 0; i < A.length; i++) { var current = A[i]; var flag = 0; for (var j = 0; j < A.length; j++) { if (current == A[j]) flag++; } if (flag == 1) output.push(current); } alert(output);
That looks an array of numbers, to me.
Anyway you could, iterating on the items of the array, use theIndexOf
method to check if it has a duplicate, stopping your iteration at first non-duplicate detection.
这篇关于如何从javascript中的字符串数组中获取非重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文