如何在JS中使用地图和VUE创建新数组
我正在尝试在JavaScript中设置MAP方法,并在VUE中创建新的对象数组,同时仅返回每个对象中的选定项目。为此,我设置了一个带有2个复选框的UI,与2个单独的对象相对应:
<div v-for="object in objects" :key="object.id" class="max-w-sm rounded overflow-hidden shadow-lg justify-center">
<div class="px-6 py-4">
<div class="font-bold text-xl mb-2">{{ object.name }}</div>
</div>
<span class="flex-1 flex mt-8 m-2">
<span class="flex flex-col">
<input v-model="checkBoxArray" :value="object.id" @click="selectObject(object.id)" type="checkbox" class="h-5 w-5" />
</span>
</span>
</div>
const objects = [
{
id: 1,
name: 'bucky barnes',
title: 'winter soldier',
cellnum: '123-456-7890',
email: '[email protected]',
description: 'Description 1'
},
{
id: 2,
name: 'sam wilson',
title: 'falcon',
cellnum: '098-765-4321',
email: '[email protected]',
description: 'Description 2'
},
]
复选框输入上的selectObject处理程序将所选对象的ID推到CheckboxArray:
const checkBoxArray = ref([])
const selectObject = (id) => {
checkBoxArray.value.push(id)
}
然后,手表属性用于观察CheckboxArray.value的更改。 ,然后调用一个使用MAP创建一个新数组的函数,该函数针对所选对象的ID:
watch(checkBoxArray.value, () => {
const storedObjects = objects.map(val => checkBoxArray.value.find(obj => obj === val.id), ['id', 'name', 'title'])
console.log(storedObjects)
})
我的期望是创建的新数组将是一个数组,其中仅包含来自原始对象的ID,名称和标题的对象。示例:
{id: 1, name: 'bucky barnes', title: 'winter soldier'}
但是,我目前仅返回具有所选对象ID的新数组。我该如何设置地图并在Watch属性中查找方法,以创建一个新数组,其中包含ID,名称和标题的对象?
这是完整的代码:
<template>
<div>
<div v-for="object in objects" :key="object.id" class="max-w-sm rounded overflow-hidden shadow-lg justify-center">
<div class="px-6 py-4">
<div class="font-bold text-xl mb-2">{{ object.name }}</div>
</div>
<span class="flex-1 flex mt-8 m-2">
<span class="flex flex-col">
<input v-model="checkBoxArray" :value="object.id" @click="selectObject(object.id)" type="checkbox" class="h-5 w-5" />
</span>
</span>
</div>
</div>
</template>
<script setup>
import { onMounted, ref, watch } from 'vue';
const objects = [
{
id: 1,
name: 'bucky barnes',
title: 'winter soldier',
cellnum: '123-456-7890',
email: '[email protected]',
description: 'Description 1'
},
{
id: 2,
name: 'sam wilson',
title: 'falcon',
cellnum: '098-765-4321',
email: '[email protected]',
description: 'Description 2'
},
]
const checkBoxArray = ref([])
const selectObject = (id) => {
checkBoxArray.value.push(id)
}
watch(checkBoxArray.value, () => {
const storedObjects = objects.map(val => checkBoxArray.value.find(obj => obj === val.id), ['id', 'name', 'title'])
console.log(storedObjects)
})
onMounted(() => {
console.log(objects)
})
</script>
I am attempting to set up a map method in JavaScript and Vue that creates a new array of objects while returning only selected items in each object. To accomplish this, I set up a UI with 2 checkboxes corresponding to 2 separate objects:
<div v-for="object in objects" :key="object.id" class="max-w-sm rounded overflow-hidden shadow-lg justify-center">
<div class="px-6 py-4">
<div class="font-bold text-xl mb-2">{{ object.name }}</div>
</div>
<span class="flex-1 flex mt-8 m-2">
<span class="flex flex-col">
<input v-model="checkBoxArray" :value="object.id" @click="selectObject(object.id)" type="checkbox" class="h-5 w-5" />
</span>
</span>
</div>
const objects = [
{
id: 1,
name: 'bucky barnes',
title: 'winter soldier',
cellnum: '123-456-7890',
email: '[email protected]',
description: 'Description 1'
},
{
id: 2,
name: 'sam wilson',
title: 'falcon',
cellnum: '098-765-4321',
email: '[email protected]',
description: 'Description 2'
},
]
The selectObject handler on the checkbox input pushes the id of a selected object to checkBoxArray:
const checkBoxArray = ref([])
const selectObject = (id) => {
checkBoxArray.value.push(id)
}
Then, a watch property is used to watch for changes to checkBoxArray.value, then call a function that uses map to create a new array targeting the id of the selected object:
watch(checkBoxArray.value, () => {
const storedObjects = objects.map(val => checkBoxArray.value.find(obj => obj === val.id), ['id', 'name', 'title'])
console.log(storedObjects)
})
My expectation is the new array created will be an array with an object containing ONLY the id, name, and title from the original object. example:
{id: 1, name: 'bucky barnes', title: 'winter soldier'}
However, I currently only returning a new array with the id of the selected object. How can I go about setting up the map and find methods in the watch property to create a new array with an object containing ONLY id, name, and title?
Here is the full code:
<template>
<div>
<div v-for="object in objects" :key="object.id" class="max-w-sm rounded overflow-hidden shadow-lg justify-center">
<div class="px-6 py-4">
<div class="font-bold text-xl mb-2">{{ object.name }}</div>
</div>
<span class="flex-1 flex mt-8 m-2">
<span class="flex flex-col">
<input v-model="checkBoxArray" :value="object.id" @click="selectObject(object.id)" type="checkbox" class="h-5 w-5" />
</span>
</span>
</div>
</div>
</template>
<script setup>
import { onMounted, ref, watch } from 'vue';
const objects = [
{
id: 1,
name: 'bucky barnes',
title: 'winter soldier',
cellnum: '123-456-7890',
email: '[email protected]',
description: 'Description 1'
},
{
id: 2,
name: 'sam wilson',
title: 'falcon',
cellnum: '098-765-4321',
email: '[email protected]',
description: 'Description 2'
},
]
const checkBoxArray = ref([])
const selectObject = (id) => {
checkBoxArray.value.push(id)
}
watch(checkBoxArray.value, () => {
const storedObjects = objects.map(val => checkBoxArray.value.find(obj => obj === val.id), ['id', 'name', 'title'])
console.log(storedObjects)
})
onMounted(() => {
console.log(objects)
})
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有通过第二参数过滤字段:
There is no syntax of map function that filters the fields by 2nd argument:
You should filter the objects that are stored first, then use map to transform them: