let myObjects = [{
"id": 1,
"title": "my frig closed",
"description": "closed frig pla ola pla",
"point": 8989,
"user_id": 4,
"created_at": "2019-11-23T19:54:46.000Z",
"updated_at": "2019-11-23T19:54:46.000Z",
"deleted_at": null
},
{
"id": 4,
"title": "miu you inc",
"description": "closed frig pla ola pla",
"point": 6767,
"user_id": 6,
"created_at": "2019-11-23T19:58:08.000Z",
"updated_at": "2019-11-23T19:58:08.000Z",
"deleted_at": null
}
];
var myApp = myApp || {};
myApp.functions = {
indexOf: function(myArray, searchTerm, property) {
for (var i = 0; i < myArray.length; i++) {
if (myArray[i][property] === searchTerm) return i;
}
return -1;
},
indexAllOf: function(myArray, searchTerm, property) {
var ai = [];
for (var i = 0; i < myArray.length; i++) {
if (myArray[i][property] === searchTerm) ai.push(i);
}
return ai;
},
lookup: function(myArray, searchTerm, property, firstOnly) {
var found = [];
var i = myArray.length;
while (i--) {
if (myArray[i][property] === searchTerm) {
found.push(myArray[i]);
if (firstOnly) break; //if only the first
}
}
return found;
},
lookupAny: function(myArray, searchTerm, property, firstOnly) {
var found = [];
var i = myArray.length;
while (i--) {
if (myArray[i][property].indexOf(searchTerm) !== -1) {
found.push(myArray[i]);
if (firstOnly) break; //if only the first
}
}
return found;
},
lookupAll: function(myArray, searchTerm, property) {
return this.lookup(myArray, searchTerm, property, false);
},
remove: function(myArray, searchTerm, property, firstOnly) {
for (var i = myArray.length - 1; i >= 0; i--) {
if (myArray[i][property] === searchTerm) {
myArray.splice(i, 1);
if (firstOnly) break; //if only the first term has to be removed
}
}
},
removeByIndex: function(myArray, index) {
myArray.splice(index, 1);
}
};
// exact
let term = "my frig closed";
let propertyName = "title";
let firstOnly = true;
let weFound = myApp.functions.lookup(myObjects, term, propertyName, firstOnly);
console.log("match:", weFound);
// a partial
let partterm = "pla";
propertyName = "description";
firstOnly = false;
let weFoundPart = myApp.functions.lookupAny(myObjects, partterm, propertyName, firstOnly);
console.log("match Contains:", weFoundPart);