4

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"}]
1

5 Answers 5

2

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

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

4 Comments

Thanks @mickl for quick response. I am trying to remove duplicate in ServiceNow which does not support .find(x => ) function & operator.
@snowcoder if so, just change arrow function to normal function: function(x) {return x.name === splitlen[i].name;}
Thank you, Still no luck. Could you please provide code here. I would appreciate the help.
@snowcoder I have just updated my answer and the second solution does not use arrow function
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);

Comments

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

Comments

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

Comments

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!

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.