3

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!

Ram
  • 143,282
  • 16
  • 168
  • 197
Bob the Builder
  • 503
  • 1
  • 12
  • 33

7 Answers7

5

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.)

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • Deleting my answer out of sheer embarrassment compared to this. +1 – CodingIntrigue Sep 11 '13 at 12:25
  • Ha, thanks @RGraham. This _is_ nice and short, but not necessarily efficient using `.indexOf()` inside the `.filter()` callback like that. Still, it's the first idea that came to mind and like I said, it's _one_ way to do it. – nnnnnn Sep 11 '13 at 12:29
  • 1
    I don't think there are enough fruits on the planet for it to make a difference anyway :) – CodingIntrigue Sep 11 '13 at 12:30
  • Please see this link as this is my problem - http://31.222.187.42/hca-consulting/mobile/hca.txt – Bob the Builder Sep 11 '13 at 15:22
  • @CoolDude - Why do we need to follow a link to see your problem? You've explained the problem in your question, and several people have provided answers that will produce the output you asked for. – nnnnnn Sep 11 '13 at 21:55
0

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);
}
aliensurfer
  • 1,600
  • 3
  • 17
  • 38
0

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]);
    }
});
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
rohitcopyright
  • 362
  • 1
  • 2
  • 9
0

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)
Rajarshi Das
  • 11,778
  • 6
  • 46
  • 74
  • What happened to splitting the input strings up? – nnnnnn Sep 11 '13 at 12:36
  • you means you want only last element of that array after split? – Rajarshi Das Sep 11 '13 at 12:37
  • I'm not the OP, but the question indicates that the second array element `"orange,apple"` should be split up into `"orange"` and `"apple"`, and similar for the third array element. Then after that only the unique strings should be kept. – nnnnnn Sep 11 '13 at 12:41
0

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/

geedubb
  • 4,048
  • 4
  • 27
  • 38
0

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);
Andy
  • 61,948
  • 13
  • 68
  • 95
0

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"]
Community
  • 1
  • 1