8***@qq.com
8***@qq.com
  • 发布:2024-04-26 10:15
  • 更新:2024-04-26 10:54
  • 阅读:127

methods里用for循环遍历数组里的对象,提示未定义请教一下大佬感谢

分类:uni-app

return里面的数据
arrlistph: [{
id: 1,
title: "物业",
phone: 155555555
},
{
id: 2,
title: "物业",
phone: 1555555555
}],

searchConfirm(e){  
            let that=this;  
            console.log(this.arrlistph[0].title);放了for循环外面title就能征程访问  
            console.log(e)  
            for(let i=0; i<=this.arrlistph.length; i++){  
                console.log(this.arrlistph[i].title);放了for循环里面title就提示未定义   这该怎么写喃,请教一下大佬。感谢              
                }                     
            }  
        }
2024-04-26 10:15 负责人:无 分享
已邀请:
爱豆豆

爱豆豆 - 办法总比困难多

你这个数组在默认只有2条数据 但是你循环却用了<= length
默认length=2 循环是从0开始的 会循环3次 0 1 2 但实际上你只有 0 1是存在的
所以是你的判断写错了 把=删掉即可
for(let i=0; i<this.arrlistph.length; i++) 就可以了

爱豆豆

爱豆豆 - 办法总比困难多

searchConfirm 是在哪里调用的
你发个完整的vue文件出来 帮你看下

  • 8***@qq.com (作者)

    上面自己定义了个点击事件 感谢☺

    2024-04-26 10:27

8***@qq.com

8***@qq.com (作者)

<template>
<view class="home">
<view class="sousuo">
<view class="linli ">
<input class="fs24 cola flex-grow-1" v-model="q" @focus="searchClick" placeholder="请输入搜索的内容"></input>
<view class="xilu_search_btn btn1" @click="searchConfirm(q)">搜索</view>
</view>
</view>
<view >
<view class="fs45 mt25 ">搜索结果</view>
<view>
{{re}}
</view>
</view>
<view style="margin-top: 30rpx;">
<view></view>
<view class="homeson" v-for="(item,index) in arrlistph" :key="item.id">
<view>
{{index+1}}.
</view>
<view>
{{item.title}}
</view>
<view>
{{item.phone}}
</view>
<view @click="deletes(index)">删除</view>
</view>
</view>
部门<input class="inputs" type="text" v-model="bumen" />
电话<input class="inputs" type="number" v-model="tphone" />

    <button type="primary" size="mini" @click="pushs()">增加</button>  
    <button type="primary" size="mini" @click="fabu()">发布</button>  
    <button type="primary" size="mini" @click="test()">测试</button>  
</view>  

</template>

<script>
export default {
data() {
return {
q:"",
re:"",
arrlistph: [{
id: 1,
title: "物业",
phone: 15555555
},
{
id: 2,
title: "物业",
phone: 115555555
},
],
bumen: "",
tphone: ""
};
},
methods: {
test(){
console.log(this.arrlistph[0].title)
},
pushs() {
let that = this
this.arrlistph.push({
id: Date.now(),
title: this.bumen,
phone: this.tphone
})
// console.log(this.arrlistph)
},
deletes(index) {
this.arrlistph.splice(index, 1)
},
searchClick() {
this.showStatus = true;
},
searchConfirm(e){
let that=this;
console.log(this.arrlistph[0].title);
console.log(e)
for(let i=0; i<=this.arrlistph.length; i++){
console.log(this.arrlistph[i].title);
}
}
}
}
</script>

研究生补鸭蛋

研究生补鸭蛋

for(let i=0; i<=this.arrlistph.length; i++){
console.log(this.arrlistph[i].title);
}。

最后一次循环 i === 2. this.arrlistph[2]肯定undefined

  • 8***@qq.com (作者)

    明白了,感谢感谢

    2024-04-26 10:39

要回复问题请先登录注册