0
var newarray= 
[ { value: 'Large', name: 'Size' },
{ value: 'Red', name: 'Color' },
{ value: 'Cotton', name: 'Material' },
{ value: 'Affinity', name: 'Collection' },
{ value: 'Pine Montage', name: 'Style' },
{ value: 'Large', name: 'Size' },
{ value: 'Red', name: 'Color' },
{ value: 'Jute', name: 'Material' },
{ value: 'Affinity', name: 'Collection' },
{ value: 'Pine Montage', name: 'Style' },
{ value: 'Large', name: 'Size' },
{ value: 'Green', name: 'Color' },
{ value: 'Jute', name: 'Material' },
{ value: 'Affinity', name: 'Collection' },
{ value: 'Pine Montage', name: 'Style' } ];

Here is my array i need to find unique array of object with non-repeated values in an array ,Please help

3
  • So perhaps loop through the input array, adding items to another array if they're not already present? Where are you stuck? Commented Mar 5, 2016 at 7:08
  • ok let me try this ! thanks for your suggestion Commented Mar 5, 2016 at 7:10
  • can you tell me how i will compare object of new temp array and the old array Commented Mar 5, 2016 at 7:15

2 Answers 2

1

I suggest to iterate over the array and filter the items with a look up if the item is stored in a hash map.

var array = [{ value: 'Large', name: 'Size' }, { value: 'Red', name: 'Color' }, { value: 'Cotton', name: 'Material' }, { value: 'Affinity', name: 'Collection' }, { value: 'Pine Montage', name: 'Style' }, { value: 'Large', name: 'Size' }, { value: 'Red', name: 'Color' }, { value: 'Jute', name: 'Material' }, { value: 'Affinity', name: 'Collection' }, { value: 'Pine Montage', name: 'Style' }, { value: 'Large', name: 'Size' }, { value: 'Green', name: 'Color' }, { value: 'Jute', name: 'Material' }, { value: 'Affinity', name: 'Collection' }, { value: 'Pine Montage', name: 'Style' }],
    unique = function (a) {
        var o = {};
        return a.filter(function (b) {
            var k = b.name + '|' + b.value;
            if (!(k in o)) {
                o[k] = true;
                return true;
            }
        });
    }(array);

document.write('<pre>' + JSON.stringify(unique, 0, 4) + '</pre>');

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

Comments

0

You can use Set for getting unique values

var uniq = Array.from(new Set(newarray.map(function(a) {
    return JSON.stringify(a);
}))).map(function(a) {
    return JSON.parse(a)
});

console.log(uniq); // will print your unique values

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.