Search во Vue js с перебором массива
Не могу перебрать массив из объектов и выдать значение name, при использовании npm json-server db.json Есть 2 метода нужно чтобы они работали вместе в одном li и searchFilter() как-то кривовато со всем этим работает
Вот скрины с консоли
db.json выглядит так
{
"todos": [
{
"id": 0,
"name": "Михаил Жванецкий"
},
{
"id": 1,
"name": "Виктор Цой"
},
{
"id": 2,
"name": "Петр Гланц"
}
]
}
App.vue выглядит так
<template>
<div id="app">
<h1>FindBase</h1>
<input type="text" v-model="todoName" @keyup.enter="addTodo" placeholder="Add Something" />
<input type="text" placeholder="Find" v-model="search" />
<ul>
<li v-for="todo in todos" :key="todo.id">{{todo.name}}</li>
</ul>
</div>
</template>
<script>
import axios from "axios";
const baseURL = "http://localhost:3000/todos";
export default {
name: "app",
data() {
return {
todos: [],
todoName: "",
search: "",
};
},
watch: {
searchKeyword() {
if (!this.keyword) return;
this.debounceName();
},
},
async created() {
try {
const res = await axios.get(baseURL);
this.todos = res.data;
console.log(this.todos);
} catch (e) {
console.error(e);
}
},
computed: {
searchFilter() {
return this.todos.forEach(function (arrayItem) {
return arrayItem.toLowerCase().indexOf(this.search !== -1);
});
},
},
methods: {
async addTodo() {
const res = await axios.post(baseURL, { name: this.todoName });
this.todos = [...this.todos, res.data];
this.todoName = "";
},
},
};
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #6c5ce7;
margin-top: 60px;
}
li {
list-style: none;
text-align: center;
}
h1 {
color: #a29bfe;
}
input {
text-align: center;
}
</style>
