3

I am trying to remove duplicate JSON Objects from the array in ServiceNow. Tried below code but it does not remove the duplicate. I want to compare both name & city.

var arr1 = '[{"name":"Pune","city":"India"},{"name":"Pune","city":"India"}]';
var splitlen = JSON.parse(arr1);
alert(splitlen.length);

var uniqueArray = [];
var uniqueJson = {};
for(i=0;i<splitlen.length;i++)
    {
        
        if(uniqueArray.indexOf(splitlen[i].name)==-1)
            {
                uniqueArray.push(splitlen[i]);
            }
    }

alert(JSON.stringify(uniqueArray));

Expected output :

[{"name":"Pune","city":"India"}]
snowcoder
  • 481
  • 1
  • 9
  • 23

5 Answers5

1

uniqueArray.indexOf doesn't work because you're comparing objects against strings (splitlen[i].name). Try to use .find() instead:

var arr1 = '[{"name":"Pune","city":"India"},{"name":"Pune","city":"India"}]';
var splitlen = JSON.parse(arr1);

var uniqueArray = [];
var uniqueJson = {};
for(i=0;i<splitlen.length;i++)
    {
        
        if(!uniqueArray.find(x => x.name === splitlen[i].name))
            {
                uniqueArray.push(splitlen[i]);
            }
    }

console.log(uniqueArray);

or

var arr1 = '[{"name":"Pune","city":"India"},{"name":"Pune","city":"India"}]';
var splitlen = JSON.parse(arr1);

function compare(x){
   return x.name === splitlen[i].name;
}

var uniqueArray = [];
var uniqueJson = {};
for(i=0;i<splitlen.length;i++)
    {
        
        if(!uniqueArray.find(compare))
            {
                uniqueArray.push(splitlen[i]);
            }
    }

console.log(uniqueArray);
mickl
  • 48,568
  • 9
  • 60
  • 89
  • Thanks @mickl for quick response. I am trying to remove duplicate in ServiceNow which does not support .find(x => ) function & operator. – snowcoder Jun 23 '20 at 02:36
  • @snowcoder if so, just change arrow function to normal function: `function(x) {return x.name === splitlen[i].name;}` – Yonggoo Noh Jun 23 '20 at 02:38
  • Thank you, Still no luck. Could you please provide code here. I would appreciate the help. – snowcoder Jun 23 '20 at 02:50
  • @snowcoder I have just updated my answer and the second solution does not use arrow function – mickl Jun 23 '20 at 02:51
0

you can try this. Also one more thing your array declaration is not right, remove single quotes from array.

    var arr1 = [{"name":"Pune","city":"India"},{"name":"Pune","city":"India"}];


    function getUniqueListByKey(arr, key) {
         return [...new Map(arr.map(item => [item[key], item])).values()]
     }

     var arr2 = getUniqueListByKey(arr1, "name")
     console.log(arr2);
prateek3636
  • 155
  • 1
  • 7
0

Please try the following example

const arr1 = '[{"name":"Pune","city":"India"},{"name":"Pune","city":"India"}]';
const splitlen = JSON.parse(arr1);

const output = splitlen.reduce((previousValue, currentValue) => {
  const { name, city } = currentValue;
  const index = previousValue.findIndex(
    (entry) => entry.name === name && entry.city === city
  );

  if (index === -1) {
    return [...previousValue, currentValue];
  }

  return previousValue;
}, []);

console.log(output);

See

Mario
  • 4,784
  • 3
  • 34
  • 50
0

Put the records in a hashset. If there is collision in the hashset, there is duplicate. This approach is O(n) while comparing all pairs is $O(n^2)$.

user1288043
  • 181
  • 1
  • 3
  • 10
0

I'm trying to get an answer, here's my idea:

Create a function to compare two objects then create a function to get the unique value

    function isEquals(obj1, obj2) {
    
        const aProps = Object.getOwnPropertyNames(obj1);
        const bProps = Object.getOwnPropertyNames(obj2);
        if (aProps.length !== bProps.length) {
            return false;
        }
    
        for (let j = 0; j < aProps.length; j++) {
            const propName = aProps[j];
            if (JSON.stringify(obj1[propName]) !== JSON.stringify(obj2[propName])) {
                return false;
            }
        } return true;
    
    }
  function getUnique(arr) {
            var uniqueArray = [];
            for (var item of arr) {
                const uniqueItems = arr.filter(i => isEquals(item, i));
                if (uniqueItems.length !== 0) {
                    uniqueArray.push(Object.assign({}, uniqueItems.shift()));
                }
                arr = arr.filter(i => !isEquals(item, i));
            }
            return uniqueArray;
        } 

Hope it helps!