-2

How do I check if a particular object exist or not in a JavaScript array of objects?

// Main array of objects
var dailogMappingData = [
    {
        "name": "AgglomerativeCluster",
        "size": 3938
    }, {
        "name": "CommunityStructure",
        "size": 3812
    }, {
        "name": "HierarchicalCluster",
        "size": 6714
    }
];


// search object to find in the above array is exist or not: 
var findObj = {
    "name": "CommunityStructure",
    "size": 3812
};


var m, n;

for (m = 0; m < this.dailogMappingData.length; m++) {
    for (n = 0; n < mapKeys.length; n++) {
        if (this.dailogMappingData[m][mapKeys[n]] === newObj[mapKeys[n]]) {
            isInArray = true;
        } else {
            isInArray = false;
        }

    }
    if (isInArray) {
        break;
    }
}

How to find an object in an array, or find out if it exists or not?

dferenc
  • 7,918
  • 12
  • 41
  • 49
  • Possible duplicate of [Find object by id in an array of JavaScript objects](https://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of-javascript-objects) – Anurag Srivastava Jul 25 '19 at 11:38
  • Possible duplicate of [How to determine equality for two JavaScript objects?](https://stackoverflow.com/questions/201183/how-to-determine-equality-for-two-javascript-objects) – Félix Jul 25 '19 at 11:39
  • /initialize the flag var flag =true; for(var i=0;i>dailogMappingData.length;i++){ //Getting one by one object from array var obj1 = dailogMappingData[i]; if(Object.keys(obj1).length==Object.keys(obj2).length){ for(key in obj1) { if(obj1[key] == obj2[key]) { continue; }else { flag=false; break; } } }else{ flag=false; } } – mariappan k Jul 25 '19 at 11:58
  • Need to compare all properties in the object – Voolapati Manohar Jul 25 '19 at 12:25

1 Answers1

0

if name property is unique you can simply check if there are any objects in array with the same name:

var dailogMappingData = [{
  "name": "AgglomerativeCluster",
  "size": 3938
}, {
  "name": "CommunityStructure",
  "size": 3812
}, {
  "name": "HierarchicalCluster",
  "size": 6714
}];

var findObj = {
  "name": "CommunityStructure",
  "size": 3812
};

const isInArray = dailogMappingData.some(item => item.name === findObj.name);

console.log(isInArray);

If you can't rely on name property and need to compare all properties, you may want to use isEqual function from Lodash

var dailogMappingData = [{
  "name": "AgglomerativeCluster",
  "size": 3938
}, {
  "name": "CommunityStructure",
  "size": 3812
}, {
  "name": "HierarchicalCluster",
  "size": 6714
}];

var findObj = {
  "name": "CommunityStructure",
  "size": 3812
};

const isInArray = dailogMappingData.some(item => _.isEqual(findObj, item));

console.log(isInArray);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>
antonku
  • 7,377
  • 2
  • 15
  • 21
  • expecting the second option but without Lodash can we do it with javascript – Voolapati Manohar Jul 25 '19 at 12:22
  • In this case I would suggest checking answers to this [question](https://stackoverflow.com/questions/201183/how-to-determine-equality-for-two-javascript-objects) – antonku Jul 25 '19 at 12:32