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!
Share Improve this question edited Sep 11, 2013 at 12:20 Ram 145k16 gold badges172 silver badges200 bronze badges asked Sep 11, 2013 at 12:12 Bob the BuilderBob the Builder 5433 gold badges12 silver badges35 bronze badges 9- stackoverflow./questions/9229645/… – Grumpy Commented Sep 11, 2013 at 12:16
- 1 What does this have to do with JSON? – nnnnnn Commented Sep 11, 2013 at 12:16
- stackoverflow./a/11247412/1823841 – palaѕн Commented Sep 11, 2013 at 12:17
- 1 Give Underscore a try; underscorejs/#uniq – Stefan Commented Sep 11, 2013 at 12:19
- You want to break the original array of strings into an array of words with duplicates removed? – David Thomas Commented Sep 11, 2013 at 12:20
7 Answers
Reset to default 5Here'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]);
}
});
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/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);
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)
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"]