Currently, I have a situation in ES6 where I am utilizing Vue.js for the current module.
The task at hand is to verify if a specific string value exists within an array object.
let obj1 = [{name: "abc ced", id: 1},
{name: "holla' name", id: 2},
{name: "3' name", id: 3}]
let obj2 = { key: "3' name" , key1: 2 }
The goal is to retrieve the object from obj1 where the name property value of obj2 can be found in obj1. Here's how it's being attempted:
_.each(obj1, function(obj){
for (var k in obj) {
if (!obj.hasOwnProperty(k)) continue
if (obj[k] === obj2.key) {
console.log(obj)
}
}
})
Is something being overlooked? The console never seems to log any values.
Important to note:
I am making use of lodash, and even tried using the find method as well.
let result = _.find(obj1 , {name: obj2.key})
console.log(result)
This approach works perfectly when trying:
let result = _.find(obj1 , {id: obj2.key1})
console.log(result)
So, integer matches work fine, but there are issues with strings not appearing on the console. I then tried other solutions as mentioned above.
Note: Initially everything seemed to be functioning correctly, except that I forgot to consider string case sensitivity. There were some uppercase and capitalized words causing problems. It's important to convert comparable strings to lower or uppercase so as to avoid such mistakes.
Cheers!