vue 数组操作

  1 <template>
  2 <div>
  3   <h3>所有注册用户</h3>
  4   <ul>
  5     <li v-for="(item,index) in list" v-bind:key="index">
  6  {{item}}
  7     </li>
  8   </ul>
  9
 10   <p>变异方法,改变原来的数组</p>
 11   <button v-on:click="addsplice">splice</button>
 12   <button v-on:click="addpop">pop</button>
 13   <button v-on:click="addpush">push</button>
 14   <button v-on:click="addshift">shift</button>
 15   <button v-on:click="addunshift">unshift</button>
 16   <button v-on:click="addset">set</button>
 17   <button v-on:click="addsort">sort</button>
 18   <button v-on:click="addreverse">reverse</button>
 19   <button v-on:click="addremove">remove</button>
 20   <button v-on:click="obset">objset</button>
 21   <p>非变异方法,不改变原来的数组,但是会返回一个数组</p>
 22   <button v-on:click="addfilter1">filter</button>
 23   <button v-on:click="addfilter2">filter2</button>
 24   <button v-on:click="addcontant">contant</button>
 25   <button v-on:click="addslice">slice</button>
 26   <button v-on:click="addmap">map</button>
 27   <button v-on:click="addinclude">include</button>
 28   <button v-on:click="addevery">evey</button>
 29   <button v-on:click="addsome">some</button>
 30   <button v-on:click="addfind">find</button>
 31   <button v-on:click="addreduce">reduce</button>
 32   <button v-on:click="addfor">for</button>
 33   <button v-on:click="addforeach">foreach</button>
 34   <button v-on:click="addforof">forof</button>
 35   <button v-on:click="addforobj">forobj</button>
 36 </div>
 37 </template>
 38 <script>
 39 export default{
 40   data(){
 41     return {
 42        list:[{messsage:"aa",username:"ff"},{messsage:"bb",username:"fb"}],
 43        items:[{id:1},{id:2},{id:3},{id:4}],
 44        rr:{  lie:""},
 45        num:[1,3,2,8,5,9,0]
 46       }
 47 },
 48 methods:{
 49 addsplice(){//有问题,居然把单词splice拼成了slice,结果导致一直报错,也是醉了,调了一天多
 50    //let s=this.list.slice(0)
 51    // let ff={messsage:"ab",username:"fg"}
 52    // s[2]=ff
 53    // this.list=s
 54    console.log(this.list)
 55    this.list.splice(1,0,{mee:"llll"})
 56    var jj=JSON.stringify(this.list)
 57     console.log(JSON.stringify(this.list))
 58     console.log(JSON.parse(jj))
 59 },
 60 addpop(){
 61   this.list.pop();
 62   console.log(JSON.stringify(this.list))
 63 },
 64 addpush(){
 65   this.list.push({messsage:"ss"});
 66   console.log(JSON.stringify(this.list))
 67 },
 68 addshift(){
 69   this.list.shift();
 70   console.log(JSON.stringify(this.list))
 71 },
 72 addunshift(){
 73   this.list.unshift({messsage:"ss"});
 74   console.log(JSON.stringify(this.list))
 75 },
 76 addset(){
 77    this.$set(this.list,0,{messsage:"11"})
 78   console.log(JSON.stringify(this.list))
 79 },
 80 addsort(){
 81   console.log(this.num)
 82   this.num.sort()
 83   console.log(this.num)
 84 },
 85 addreverse(){//倒序输出数组,适用于对象数组和元素数组
 86   console.log(JSON.stringify(this.list))
 87   this.list.reverse()
 88   console.log(JSON.stringify(this.list))
 89  // console.log(this.list)
 90 },
 91 addremove(){//无效?????
 92    this.list.$remove({message:"11"})
 93   console.log(JSON.stringify(this.list))
 94 },
 95 obset(){
 96 this.$set(this.rr,"lie","kkk")
 97  console.log(JSON.stringify(this.rr))
 98 },
 99 addfilter1(){
100 this.list=this.list.filter(function(item){
101   return item.messsage
102 })
103  console.log(JSON.stringify(this.list))
104 },
105 addfilter2(){
106   var gh= this.num.filter(function(number){
107     return number%2===0
108   })
109  console.log(JSON.stringify(gh))
110 },
111 addcontant(){
112  console.log(this.num)
113  var dd=this.num.concat(2,4,5)
114  console.log(dd)
115   var dd=this.num.concat([2,4,5],[2,3,4])
116  console.log(dd)
117 },
118 addslice(){
119   var dd=this.num.slice()//返回所有元素
120   console.log(dd)
121   var dd=this.num.slice(1,2)//返回index为1的值
122   console.log(dd)
123   var dd=this.num.slice(-3,1)//空值
124   console.log(dd)
125    var dd=this.num.slice(-3)//倒数三个元素
126   console.log(dd)
127 },
128 addmap(){
129   console.log(this.num)
130  let dd=this.num.map(function(item,index){return item*=3})
131   console.log(dd)
132 },
133 addinclude(){
134   console.log(this.num)
135  let d1=this.num.includes(5)
136   console.log(d1)
137    let d2=this.num.includes(20)
138   console.log(d2)
139 },
140 addevery(){
141   console.log(this.num)
142  let d1=this.num.every(
143    function(item,index){
144      console.log(item)
145    return item.toString().indexOf(1)>-1
146    })
147   console.log(d1)
148 },
149 addsome(){
150  let d1=this.num.some(function(item,index)
151  {
152       console.log(item)
153    return item.toString().indexOf(1)>-1
154    })
155   console.log(d1)
156 },
157 addfind(){
158   console.log(this.num)
159  let d1=this.num.find(function(item,index){
160    return item.toString().indexOf(3)>-1
161  })
162   console.log(d1)
163 },
164 addreduce(){
165   console.log(this.num)
166  let d1=this.num.reduce(function(item,index){
167    return item.toString().indexOf(3)>-1
168  })
169   console.log(d1)
170 },
171 addfor(){
172   console.log(this.num)
173  for(let i=0;i<this.num.length;i++)
174   console.log(this.num[i])
175 },
176 addforeach(){
177   console.log(this.num)
178  this.num.forEach(function(item,index){console.log(item)})
179 },
180 addforof(){
181   console.log(this.num)
182   for(let d1 of this.num){console.log(d1)}
183 },
184 addforobj(){
185   let obj={school:"name",age:8}
186   for(let d1 of Object.keys(obj)){console.log(d1+":"+obj[d1])}
187 },
188 }
189 }
190 </script>

原文地址:https://www.cnblogs.com/Zhengxiaoxiao/p/10823150.html

时间: 2024-07-28 18:35:45

vue 数组操作的相关文章

关于Vue数组操作

Vue的数组操作的实现代码大致如下: const aryMethods = ['push', 'pop', 'shift', 'unshift', 'splice', 'sort', 'reverse']; const arrayAugmentations = []; aryMethods.forEach((method)=> { // 这里是原生Array的原型方法 let original = Array.prototype[method]; // 将push, pop等封装好的方法定义在对

Vue数组操作不刷新视图问题的解决

最近使用Vue2.0开发项目,有一个列表使用v-for绑定到数组,按照Vue的推荐方案,使用push.splice.this.$set三个变异方法操作数组.发现在添加页面,三个方法都能及时刷新视图:但是在修改页面,只有this.$set方法修改数组元素能刷新,其他两个方法死活不刷新视图,使用Vue-devtools查看,数据都能成功修改. 尝试了在mount阶段就用push方法一个个添加,尝试把用到的对象都显示定义字段属性,尝试百度到的N种方法...花了四五个小时,都不行!但问题肯定是出在没有触

vue数组操作不更新视图问题

vue 观察数组的变异方法 更新视图 push() pop() shift() unshift() splice(i,n,arr) sort(xx) reverse() ex: app.book.push({ name:'css', author:'lee' }) 有些方法不会改变数组 filter() concat() slice() 返回新数组  需要用 新返回的数组 更新原数组 app.books= app.books.filter(functiion(item){ return item

vue 数组

今天项目中发现的一个问题: 在vue项目中输出一个数组,明明有俩个值:0,6,但是length为1 正常的是这样的 结果研究发现,是vue源码的问题,具体内容如下: 转载自:http://www.cnblogs.com/Darlietoothpaste/p/6682407.html Vue的数组操作的实现代码大致如下: 1 const aryMethods = ['push', 'pop', 'shift', 'unshift', 'splice', 'sort', 'reverse']; 2

Vue数组双向绑定内幕

在Vue使用期间,我们经常会遇到对于数组的操作,但是Vue中如何实现数组双向绑定的呢,在这篇PPT中,首先回顾Vue双向绑定原理,之后再去研究Vue对于数组的特殊处理. ppt:Vue数组双向绑定内幕 参考:https://ustbhuangyi.github.io/vue-analysis/ 原文地址:https://www.cnblogs.com/MarphyDemon/p/11487764.html

PHP:数组操作函数array_count_values()的实现

    PHP作为一门弱类型的脚本语言,其变量无需声明,即用即得,其数组更是与其他强类型语言相差巨大,比如PHP数组中的关联键和数值键,其中最有趣的莫过于关联键了,这是在C++或JAVA中无法见到的,而且PHP还提供诸多强大的数组操作函数,比如   array_values()//可以剥离数组中的关联键和数值键,或得有其元素的值所组成的数组 array_keys()//获得所有的关联键和数值键  利用这两个函数就可以非常方便简单的实现  array_count_values()函数  思路就是先

PHP内核探索之变量(4) - 数组操作

上一节(PHP内核探索之变量(3)- hash table),我们已经知道,数组在PHP的底层实际上是HashTable(链接法解决冲突),本文将对最常用的函数系列-数组操作的相关函数做进一步的跟踪. 本文主要内容: PHP中提供的数组操作函数 数组操作函数的实现 结语参考文献 一.PHP中提供的数组操作函数 可以说,数组是PHP中使用最广泛的数据结构之一,正因如此,PHP为开发者提供了丰富的数组操作函数(参见http://cn2.php.net/manual/en/ref.array.php

PHP:常用PHP数组操作函数

php为我们提供了丰富的数组操作函数,用这些函数可以非常方便的实现我们所想实现的功能. 添加和删除数组元素 添加元素的方法 array_unshift(array array,mixed var [,mixed var...]) 在数组头添加元素,所有的数值键会被相应的修改,但关联键不会受到影响 array_push(array array,mixed var [,mixed var.....]) 在数组尾添加元素 删除数组元素的方法 array_shift(array array) 删除数组头

js数组操作

js数组操作大全(转) shift:删除原数组第一项,并返回删除元素的值:如果数组为空则返回undefined var a = [1,2,3,4,5]; var b = a.shift(); //a:[2,3,4,5] b:1 unshift:将参数添加到原数组开头,并返回数组的长度 var a = [1,2,3,4,5]; var b = a.unshift(-2,-1); //a:[-2,-1,1,2,3,4,5] b:7 注:在IE6.0下测试返回值总为undefined,FF2.0下测试