-4

I am getting an array like

["cat, Kitchen", "dog", "Kitchen, dog", "Kitchen"].

How can I get a unique array from this. I have tried split(', '), but I got the error .split is not a function in my browser.

I want the final array to be:

["cat", "Kitchen", "dog"]

Please Let me know how to write jQuery for this output

Thanks in Advance....

1
  • 2
    is the missing quotes after cat and before the 2nd dog intentional? Commented Jun 12, 2014 at 6:46

6 Answers 6

2

jQuery.unique(["cat", "Kitchen", "dog", "Kitchen", "dog", "Kitchen"].join(",").split(","))

Sign up to request clarification or add additional context in comments.

1 Comment

how does this answer makes any sense for the question?
1
function unique(array) {
    if (array) {
        var found = {};
        array = array.join(",").split(",").filter(function (x) {
            x = x.trim();
            return (found[x] ? false : (found[x] = x));
        })
    }
    return array;
}

console.log(unique(["cat, Kitchen", "dog", "Kitchen, dog", "Kitchen"]));

demo : http://jsfiddle.net/diode/2P2Kg/

Tested only in Chrome, so not sure whether this will work in all browsers. May not be the perfect solution, but this is the direction in which you can proceed.

Comments

1

Try this Demo Fiddle,

var arr = ["cat", "Kitchen", "dog", "Kitchen", "dog", "Kitchen"];
unique(arr);


function unique(arrList) {
    var final = [];
    $.each(arrList, function (i, e) {
        if ($.inArray(e, final) == -1) final.push(e);
    });
    console.log(final);
}

Note :

The way you declared your array is wrong - ["cat, Kitchen", "dog", "Kitchen, dog", "Kitchen"]. It has no duplicate elements - "cat, Kitchen" is counted as one.

It should be ["cat", "Kitchen", "dog", "Kitchen", "dog", "Kitchen"].

Comments

1

1) Missed to close the double quotes in array list.

2) Use the below function to get the unique array in javascript

var uniqueArray =["cat", "Kitchen", "dog", "Kitchen", "dog", "Kitchen"];

function GetUnique(inputArray)
{
    var outputArray = [];

    for (var i = 0; i < inputArray.length; i++)
    {
        if ((jQuery.inArray(inputArray[i], outputArray)) == -1)
        {
            outputArray.push(inputArray[i]);
        }
    }

    return outputArray;
};

alert(GetUnique(uniqueArray))

DEMO

or use with jquery

var uniqueArray = ["cat", "Kitchen", "dog", "Kitchen", "dog", "Kitchen"];

alert($.unique(uniqueArray));

DEMO

Comments

0
var streetaddress_hs = ["cat", "Kitchen", "dog", "Kitchen", "dog", "Kitchen"];
var streetaddress2 = [];
$.each(streetaddress_hs, function (i, el) {
    if ($.inArray(el, streetaddress2) === -1) streetaddress2.push(el);
});
availableTags = streetaddress2;
alert(availableTags);

Comments

0

EDIT: Expanding on the accepted answer (deduplicating entries):

["cat, Kitchen", "dog", "Kitchen, dog", "Kitchen"].join(",").split(",").
  map(function(entry){return entry.trim();}).
  filter(function(entry, i, a) { return a.indexOf(entry) === i; });

I solved it with a series of transformations, no need for jQuery. Couldn't name it better, though, as I don't know what's the real deal.

function doItsThing (crazyArray) {

  function toArrays (entry) {
    return entry.split(',');
  }
  function toAFlatArray (arrays, entry) {
    return arrays.concat(entry);
  }
  function trim (entry) {
    return entry.trim();
  }
  function dedupe (entry, index, array) {
    return array.indexOf(entry) == index;
  }

  return crazyArray.
    map(toArrays).
    reduce(toAFlatArray, []).
    map(trim).
    filter(dedupe);
}

doItsThing(["cat, Kitchen", "dog", "Kitchen, dog", "Kitchen"])

Or, if you want it all inlined (although not so readable, compared to the above, imo):

function doItsThing (crazyArray) {
  return crazyArray.
    map(function (entry) {
      return entry.split(',');
    }).
    reduce(function (collection, entry) {
      return collection.concat(entry);
    }, []).
    map(function (entry) {
      return entry.trim();
    }).
    filter(function (entry, index, collection) {
      return collection.indexOf(entry) == index;
    });
}

doItsThing(["cat, Kitchen", "dog", "Kitchen, dog", "Kitchen"])

Sure it can be improved, but this is the idea.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.