0

This might be easy but I am struggling with this at the moment. I need to check inside a function if the there are duplicate values in the json object.

My Object :-

"controls": {
        "score": "AppId",
        "title": "CountryCode",
        "onesubtitle": "CountryName",
        "twosubtitle": "Region",
        "summary": "CountryHead",
        "keyword": "Region"
    }

 Object.values(this.controls).forEach(val => {
      console.log(val);
    });

I need to check and return boolean if the values are duplicate in the object like the values bold below.

"controls": { "score": "AppId", "title": "CountryCode", "subtitle1": "CountryName", "subtitle2": "Region", "summary": "CountryHead", "keywords": "Region" }

1 Answer 1

1

Imperative paradigm :


let complexObject= {
        "score": "AppId",
        "title": "CountryCode",
        "onesubtitle": "CountryHead1",
        "twosubtitle": "Region",
        "summary": "CountryHead1",
        "keyword": "Region"
    }
let valueArray=[];
Object.values(complexObject).forEach(val => {
      valueArray.push(val)
    });
for(i = 0; i < valueArray.length; i++) {  
    for(j = i + 1; j < valueArray.length; j++) {  
        if(valueArray[i] == valueArray[j])  
            console.log(valueArray[i]); 
    }  
}  

Declarative paradigm :

let complexObject= {
        "score": "AppId",
        "title": "CountryCode",
        "onesubtitle": "CountryHead1",
        "twosubtitle": "Region",
        "summary": "CountryHead1",
        "keyword": "Region"
    }
let valueArray=[];
Object.values(complexObject).forEach(val => {
      valueArray.push(val)
    });

console.log(valueArray.filter((item, index) => valueArray.indexOf(item) !== index));
Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot for this. we have manipulated the code according to our needs the solution has worked very well. @farazjalili
The second one could be simplified even further, because filter function parameter has 3 parameters (value, index, array), so we don't need to build the valueArray. It could just be: Object.values(complexObject).filter((val, i, a) => a.indexOf(val) !== i)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.