I would like to parse it and find if two value exist only once within this array.
Example: "text-success" OR "muted" if EXIST ONCE return the array
if it EXIST twice retrun null
example:
Array[6]
0:"text-warning"
1:"text-success"
2:"text-warning"
3:"text-success"
4:"muted"
5:"text-warning"
Array[6]
0:"text-warning"
1:"text-warning"
2:"text-warning"
3:"text-warning"
4:"text-success"
5:"text-warning"
Array[6]
0:"text-warning"
1:"text-warning"
2:"text-warning"
3:"text-success"
4:"muted"
5:"text-warning"
Array[6]
0:"text-warning"
1:"text-warning"
2:"text-warning"
3:"text-warning"
4:"muted"
5:"text-warning"
1st one will return FALSE because both exist "text-success"-twice AND "muted"-once
2nd one will return TRUE because it exist ONCE "text-success"
3rd one will return FALSE because both exist "text-success" AND "muted"
4th one will return TRUE because it exist ONCE "muted"
. . . .
and so on
so far this is my Javascript:
function singles( array ) {
for( var index = 0, single = []; index < array.length; index++ ) {
if( array.indexOf( array[index], array.indexOf( array[index] ) + 1 ) == -1 ) single.push( array[index] );
};
return single;
};
it is not working
please help.
You could use a simple loop with a counter as follows :
var count = 0;
for(i=0;i<arr.length;i++) {
if(arr[i] == "test-success" || arr[i] == "muted") {
count++;
}
if(count == 1) {
//process array data;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.