I have the following array:
var fruits = ["orange","orange,apple","apple,orange,pear"];
I am trying to achieve the following:
fruits = ["orange","apple","pear"]
Any suggestions, thanks!
I have the following array:
var fruits = ["orange","orange,apple","apple,orange,pear"];
I am trying to achieve the following:
fruits = ["orange","apple","pear"]
Any suggestions, thanks!
Here's one way to do it:
fruits = fruits.reduce(function (p, c) {
return p.concat(c.split(","));
}, []).filter(function(e, i, a) {
return a.indexOf(e) === i;
});
(EDIT: Note that .filter()
and .reduce()
are not supported in IE8 or older, but there are shims available if you need to support older IE.)
There is no readymade way to achieve this.
What you could do is use associative arrays that act like dictionaries. Iterate through the list, read each element, split it and store it in an associative array with the key as the fruit name, and the value as the fruit name too. Something like:
fruits["orange"] = "orange";
If the value already exists in fruits[], it will simply be overwritten. At the end, you'll have as many keys in fruits[] as there are unique fruits.
EDIT:
var fruits = ["orange", "orange,apple", "apple,orange,pear"];
var a = fruits.toString().split(",");
var obj = new Object();
for (var i = 0; i < a.length; i++) {
obj[a[i]] = a[i];
}
for (var key in obj) {
alert(key);
}
Will give you the expected result.
var fruits = ["orange","orange,apple","apple,orange,pear"];
var uniqueVal = [];
$.each(fruits, function(i, el){
var splitVals = el.split(',');
for(var i=0; i<splitVals.length; i++ ){
if($.inArray(splitVals[i], uniqueVal) === -1) uniqueVal.push(splitVals[i]);
}
});
try this
var fruits = ["orange","orange,apple","apple,orange,pear"];
if you want to split the 2nd elements and concat the strings
fruits.concat(fruits[1].split(","))
or you want all the elements split and concat then uniq
var new_fruits = [];
$.each(fruits, function(i, el){
fruits.concat(fruits[i].split(","))
if($.inArray(el, new_fruits) === -1) new_fruits.push(el);
});
or using underscore.js
_.uniq(fruits, false)
This should work:
var fruits = ["orange","orange,apple","apple,orange,pear"];
var out = [];
$(fruits).each(function(i) {
var s = fruits[i].split(',');
$(s).each(function(i) {
if($.inArray(s[i], out)===-1)
{
out.push(s[i]);
}
});
});
JSFIddle: http://jsfiddle.net/rRC65/
This uses an object to capture the elements, then return its keys. And no need for shims.
function remdupes(arr) {
var obj = {}, out = [];
for (var i = 0, l = arr.length; i < l; i++) {
var el = arr[i].split(',');
for (var ii = 0, ll = el.length; ii < ll; ii++) {
obj[el[i]] = true;
}
}
for (var key in obj) { out.push(key); }
return out;
}
Note that the last couple of lines:
for (var key in obj) { out.push(key); };
return out
can be rewritten:
return Object.keys(obj);
You could do this like so (eliminateDuplicates) :
fruits = fruits.join(','); // "orange,orange,apple,apple,orange,pear"
fruits = fruits.split(','); // ["orange", "orange", "apple", "apple", "orange", "pear"]
fruits = eliminateDuplicates(fruits); // ["orange", "apple", "pear"]