I have the following object in Javascript:
Array[11]
0:"532"
1:"text-warning"
2:"51"
3:"text-warning"
4:"16"
5:"muted"
6:"35"
7:"text-warning"
8:"38"
9:"text-warning"
10:"106"
Array[11]
0:"533"
1:"text-warning"
2:"51"
3:"text-warning"
4:"16"
5:"muted"
6:"35"
7:"text-success"
8:"38"
9:"text-warning"
10:"106"
Array[11]
0:"534"
1:"text-warning"
2:"51"
3:"text-warning"
4:"16"
5:"text-warning"
6:"35"
7:"text-success"
8:"38"
9:"text-warning"
10:"106"
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 return null
if TRUE return the corresponding id
In the above example:
1st Array: is TRUE
Array[11]
0:"532"
1:"muted"
2:"35"
2nd one is FALSE because its exist twice
3rd one: is also TRUE
Array[11]
0:"534"
1:"text-success"
2:"38"
I have been trying this past few days was not successful
I have the following JQUERY: but it only gets the Unique value but discard the others:
I need the first id and the corresponding id as well:
Array[11]
0:"534" -> main id (needed)
1:"text-success"
2:"38" -> company id (needed)
function singles( array) {
for( var index = 0, single = []; index < array.length; index++ ) {
if(array[index] == "text-success" || array[index] == "muted") {
single.push(array[index]);
}
}
return single;
};
The result or the output which is required:
Array[11]
0:"532",
1:"muted",
2:"35",
The first value is the Main ID, the 2nd value is the UNIQUE and the 3rd value is the corresponding ID for the unique value.
You can solve:
- transform Object to Array
- Check duplicated element then pass
- unique element push into new array
var obj = {
0:"532",
1:"text-warning",
2:"51",
3:"text-warning",
4:"16",
5:"muted",
6:"35",
7:"text-warning",
8:"38",
9:"text-warning",
10:"106"
};
function restUnique(obj){
var mix = [];
var output= [];
Object.keys(obj).forEach(function(key) {
mix[key]=obj[key];
});
for(var i in mix){
fb=mix.indexOf(mix[i]);
fa=mix.lastIndexOf(mix[i]);
if(fb===fa){output.push(mix[i]);}
}
return output;
}
// usage example:
restUnique(obj); // output : 532,51,16,muted,35,38,106