I want to loop through a JSON Array to search for a keyword. I am refering this thread to achieve this.
entries is a JSON array of stucture
[
{"a": "something", "id": 54785, "b": ["each", "every", "one"]},
{"a": "something", "id": 54785, "b": ["each", "every", "one"]},
{"a": "something", "id": 54785, "b": ["each", "every", "one"]},
]
searchItem
came from this custom component
<FormInput type="text"
v-model="searchItem"
@input="searchObject()"
placeholder="Search here">
</FormInput>
I placed my function in methods of component like this.
searchObject: function() {
for (var i=0; i<this.entries.length; i++) {
for (var key in this.entries[i]) {
if (this.entries[i].key.indexOf(this.searchItem)!==-1) {
this.result.push(this.entries[i])
}
}
}
return this.result
}
I get this error in console
TypeError: Cannot read property 'indexOf' of undefined
When I change in computed function and try [key]
instead of .key
searchObject() {
for (var i=0; i<this.entries.length; i++) {
for (var key in this.entries[i]) {
if (this.entries[i][key].indexOf(this.searchItem)!==-1) {
this.result.push(this.entries[i])
}
}
}
return this.result
}
I am not getting anything pushed in result, neither I am getting any error in console. I tried to put console.log() command on my function, but again nothing on console.
this.result
? Where is it defined?data(){return{result:[]}}
a
,id
andb
. I would like to search for likes ofa
andb
.