javascript - Array.push() if does not exist? - Stack Overflow

PHOTO EMBED

Tue Feb 09 2021 12:59:10 GMT+0000 (Coordinated Universal Time)

Saved by @ozgunn #javascript

var arrayObj = [{name:"bull", text: "sour"},
    { name: "tom", text: "tasty" },
    { name: "tom", text: "tasty" }
]
var index = arrayObj.findIndex(x => x.name=="bob"); 
// here you can check specific property for an object whether it exist in your array or not

index === -1 ? arrayObj.push({your_object}) : console.log("object already exists")
 
content_copyCOPY

https://stackoverflow.com/questions/1988349/array-push-if-does-not-exist