locked
Remove Duplicate Object From Array RRS feed

  • Question

  • User-2132497554 posted

    Hi Folks,

    let array = [
    { label: aa, value: bb },
    { label: aa, value: bb },
    { label: dd, value: dd },
    { label: dd, value: dd },
    ];

    I want to remove duplicate label from array like this:

    array = [
    { label: aa, value: bb },
    { label: dd, value: dd },
    ];

    Any idea?

    Thanks

    Friday, April 13, 2018 4:58 AM

Answers

  • User-369506445 posted

    hi

    please try below code :

    var array = [
                 { label: 'aa', value: 'bb' },
                 { label: 'aa', value: 'bb' },
                 { label: 'dd', value: 'dd' },
                 { label: 'dd', value: 'dd' },
         ];
         b = uniqBy(array, JSON.stringify);
         debugger;
        console.log(b) // [['aa','bb'], ['dd','dd']]
    
         function uniqBy(a, key) {
             var index = [];
             return a.filter(function (item) {
                 var k = key(item);
                 return index.indexOf(k) >= 0 ? false : index.push(k);
             });
         }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 20, 2018 8:17 AM

All replies

  • User1372467857 posted

    Try this.

    array = array.Distinct().ToList();
    Friday, April 13, 2018 7:06 AM
  • User632428103 posted

    Hello all,

    @lamguy84, i think it's on jquery you'd like to make it ?

    i'm not an expert with it , but perphas a solution ...

        var initArray = [];
                var obj1 = new Object();
                obj1.label = 'aa';
                obj1.value = 'bb';
                
                var obj2 = new Object();
                obj2.label = 'aa';
                obj2.value = 'bb';
    
                var obj3 = new Object();
                obj3.label = 'dd';
                obj3.value = 'dd';
                initArray.push(obj1);
                initArray.push(obj2);
                initArray.push(obj3);
    
                //
                var distinctArray = [];
    
                for (i = 0; i < initArray.length; i++) 
                {
                    var data = initArray[i];
                    if (distinctArray.length == 0)
                        distinctArray.push(initArray[i]);
                    else
                    {
                        //browse over array destination and try to find if item already exist or not
                        $.each(distinctArray, function (index, item)
                        {
                            if (data.label == distinctArray[index].label && data.value == distinctArray[index].value) {
                                alert('OBJECT FOUD ON DESTINATION ARRAY');
                            }
                            else
                            {
                                distinctArray.push(data);
                                alert('item added');
                            }
                        });
                    }
                }

    Friday, April 13, 2018 8:21 AM
  • User-2132497554 posted

    @ameenkpn

    I am using Typescript or JavaScript. Sorry i forget to tell you which technology use it.

    Friday, April 13, 2018 11:16 AM
  • User-2132497554 posted

    @jimmy

    I am using Typescript/JavaScript......not jquery!

    Friday, April 13, 2018 11:18 AM
  • User632428103 posted

    ok but i don't know typescript if you want to do my sample in javascript ..

    just replace $.each with this =>

     for (var w = 0; w < distinctArray.length; w++)
    {
    if (data.label == distinctArray[w].label && data.value == distinctArray[w].value)
    alert('OBJECT FOUD ON DESTINATION ARRAY');
    else
    {
    distinctArray.push(data);
    alert('item added');
    }
    }

    normally all will be work i hope :)

    Friday, April 13, 2018 11:53 AM
  • User-474980206 posted

    pretty simple:

    let array =[
      { label: "aa", value: "bb" },
      { label: "aa", value: "bb" },
      { label: "dd", value: "dd" },
      { label: "dd", value: "dd" }
    ];
    
    array = Object.values(array.reduce((p,c) => {
        p[c.label] = c; 
        return p;
      },{}));
    

    Friday, April 13, 2018 2:46 PM
  • User-2132497554 posted

    I am getting error 

    Property 'values' does not exist on type 'ObjectConstructor'.

    And

    Element implicitly has an 'any' type because type '{}' has no index signature. (p[c.label] )

    Thursday, April 19, 2018 7:14 AM
  • User516094431 posted

    You can use following line of code it will work for TS as well as Javascript:

    let array = [
                { label: 'aa', value: 'bb' },
                { label: 'aa', value: 'bb' },
                { label: 'dd', value: 'dd' },
                { label: 'dd', value: 'dd' },
                ];
                function removeDuplicate(arr){
                    for(var i=0; i<arr.length; i++)
                    {
                       var result = arr.find(a => a.label == arr[i].label && a.value == arr[i].value);
                       if(result != null && result != undefined)
                       {
                           var arrSearchIndex = arr.indexOf(result);
                           arr.splice(arrSearchIndex, 1);
                       } 
                    }
                    return arr;
                }
                
                console.log(removeDuplicate(array));

    Thursday, April 19, 2018 8:14 AM
  • User563203385 posted
    let arrayToChange = [
                { label: 'a', value: 'a' },
                { label: 'b', value: 'b' },
                { label: 'c', value: 'c' },
                { label: 'd', value: 'd' },
    { label: 'c', value: 'c' },
    { label: 'e', value: 'e' },
    ]; function removeDuplicateValues(arrayToChange){ for(var i=0; i<arrayToChange.length; i++) { var searchResult = arrayToChange.find(a => a.label == arr[i].label && a.value == arr[i].value); if(result != null && result != undefined) { var indexOfArray = arrayToChange.indexOf(searchResult); arrayToChange.splice(indexOfArray , 1); } } return arrayToChange; } console.log(removeDuplicate(arrayToChange));
    Thursday, April 19, 2018 8:23 AM
  • User61956409 posted

    Hi IamGuy84,

    You can refer to the following code to remove duplicates from an array.

    <script>
        let array = [
            { label: "aa", value: "bb" },
            { label: "aa", value: "bb" },
            { label: "dd", value: "dd" },
            { label: "dd", value: "dd" },
        ];
    
        let unique_array = []
    
        unique_array.push(array[0]);
    
        (function (arr) {
    
            if (arr.length >= 1) {
                for (let i = 1; i < arr.length; i++) {
    
                    checkitem(unique_array, arr[i]);
                }
            }
        })(array);
    
        function checkitem(arr, item) {
            
            for (let i = 0; i < arr.length; i++) {
    
                if (arr[i].label == item.label && arr[i].value == item.value) {
                    return;
                } else {
                    if (i == arr.length-1) {
                        unique_array.push(item);
                    }
                }
            }
        }
    
    
        for (let i = 0; i < unique_array.length; i++) {
            console.log("label: " + unique_array[i].label + "; value: " + unique_array[i].value);
        }
    
    
    </script>

    Test Result:

    With Regards, 

    Fei Han

    Thursday, April 19, 2018 8:56 AM
  • User-369506445 posted

    hi

    please try below code :

    var array = [
                 { label: 'aa', value: 'bb' },
                 { label: 'aa', value: 'bb' },
                 { label: 'dd', value: 'dd' },
                 { label: 'dd', value: 'dd' },
         ];
         b = uniqBy(array, JSON.stringify);
         debugger;
        console.log(b) // [['aa','bb'], ['dd','dd']]
    
         function uniqBy(a, key) {
             var index = [];
             return a.filter(function (item) {
                 var k = key(item);
                 return index.indexOf(k) >= 0 ? false : index.push(k);
             });
         }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 20, 2018 8:17 AM