Aakash Thakur
Aakash Thakur

Reputation: 19

Json single Object check to see if it has duplicate values typescript angular

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

Upvotes: 0

Views: 1197

Answers (1)

Faraz jalili
Faraz jalili

Reputation: 84

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

Upvotes: 1

Related Questions