伙伴匹配系统踩坑日记4

伙伴匹配系统踩坑日记4

前端页面编写

需要实现一个搜索的searchPage页,包含搜索标签的功能,代码如下

const originTagList = [
  {
    text: '性别',
    children: [
      { text: '男', id: '男' },
      { text: '女', id: '女' },
    ],
  },
  {
    text: '年级',
    children: [
      { text: '大一', id: '大一' },
      { text: '大二', id: '大二' },
      { text: '大3', id: '大3' },
      { text: '大4', id: '大4' },
      { text: '大5', id: '大5' },
      { text: '大6', id: '大6' },
      { text: '大7', id: '大7' },
      { text: '大8', id: '大8' },
    ],
  },
]

let tagList = ref(originTagList);

const onSearch = (val) => {
  tagList.value=originTagList.map(parentTag => {
    const tempChidren=[...parentTag.children];//es6的语法,表示将parentTag.children中的元素取出,放到新的数组里
    const tempParentTag=[parentTag];
    tempParentTag.children = tempChidren.filter(item => item.text.includes(searchText.value));
    return tempParentTag;
  });
  //测试打平效果,扁平化
/*  console.log(tagList.flatMap(parentTag => parentTag.children))*/

}
const onCancel = () => {
  searchText.value='';
  tagList.value=originTagList;
}

但是报错了

image-20240805212202656

检查代码,发现是这里出错了,parentTag使用大括号包裹的,是一个对象

image-20240805212244154

改成这样

image-20240805212351535

image-20240805212330271

posted @ 2024-08-18 23:23  vast_joy  阅读(28)  评论(0)    收藏  举报