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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With