WeChat applet learns array operations
Preface
I believe that after the mini program was launched in public beta, many friends have already rushed to apply to register the mini program. During the development stage, we also encountered many problems, such as the wx.request data request was unsuccessful, I didn’t know how to push data into the array during array operations, how input monitors the status of user input, and the background-image of css could not obtain the local Resources, etc., this blog will have a special topic to provide solutions to friends who encounter these problems.
Array operation
What we are mainly talking about today is array operation.
I believe that for children who have used vuejs, reactjs and other mvvm framework, the array operation of the WeChat applet is very simple. The principle is the same.
This is a simple demo that has been uploaded to github, everyone can download it directly. We will mainly talk about some commonly used operation methods in arrays. Insert new data forward and backward into the array, modify the array, deletearray, clear the array, and some other operation methods. I will give you learning ideas. .
Demo array operation example path: /pages/<a href="http://www.php.cn/wiki/1041.html" target="_blank">array</a>/array.w<a href="http://www.php.cn/wiki/1527.html" target="_blank">xml</a>
Insert new data forward and backward
Page({ data: { list:[{ id:1, name:'应季鲜果', count:1 },{ id:2, name:'精致糕点', count:6 },{ id:3, name:'全球美食烘培原料', count:12 },{ id:4, name:'无辣不欢生猛海鲜', count:5 }] } })
We initialize some data, we need to add new data to the list, we need to use<a href="http://www.php.cn/wiki/48.html" target="_blank">JavaScript</a> concat()
Method, concat()
method is used to concatenate two or more arrays. This method does not change the existing array.
In fact, what we call forward and backward inserting data is actually combining two arrays to form a new array, and then using this.<a href="http://www.php.cn/code/8209.html" target="_blank"> set</a>Data()
can be rendered to the page.
Let’s take a look at the code in the WeChat applet.
Insert data forward demo
//向前增加数据 add_before:function (){ //要增加的数组 var newarray = [{ id:6, name:'向前增加数据--'+new Date().getTime() , count:89 }]; //使用concat()来把两个数组合拼起来 this.data.list = newarray.concat(this.data.list); //将合拼之后的数据,发送到视图层,即渲染页面 //大伙请记录,修改了数据后,一定要再次执行`this.setData()`,页面才会渲染数据的。 this.setData({ 'list': this.data.list }); }
Insert data backward demo
//向后增加数据 add_after:function (){ //要增加的数组 var newarray = [{ id:5, name:'向后增加数据--'+new Date().getTime() , count:89 }]; this.setData({ 'list':this.data.list.concat(newarray) }); },
Careful friends should find that, When these two paragraphs are combined using concat()
, the data in the concat brackets are different. The difference between inserting data forward and backward is here.
//假设这一段是我们要新增的数组 var newarray = [{ id:5, name:'向后增加数据--'+new Date().getTime() , count:89 }]; //向前--用newarray与this.data.list合拼 this.data.list = newarray.concat(this.data.list); //向后--用this.data.list与newarray合拼 this.data.list = this.data.list.concat(newarray);
Modify array
Modifying the displayed data is a very common thing in the development process.
//修改数组 edit:function (e){ //这个参数“e”的具体作用,请参考微信小程序官方提供的说明,地址为https://mp.weixin.qq.com/debug/wxadoc/dev/framework/view/wxml/event.html?t=20161107 var dataset = e.target.dataset; var Index = dataset.index; //在通过在wxml页面里使用 data-index="{{index}}"传递过来的,是为识别正在编辑修改哪个数组。 //我们要修改的数组 this.data.list[Index].name = '修改了内容'+new Date().getTime(); //将合拼之后的数据,发送到视图层,即渲染页面 //大伙请记录,修改了数据后,一定要再次执行`this.setData()`,页面才会渲染数据的。 this.setData({ list:this.data.list }); }
Delete a certain piece of data
There will be additions, changes and deletions.
Deletion requires the JavaScript splice()
method. The splice()
method adds/removes items to/from the array, and then returns the deleted item.
//删除 remove:function (e){ var dataset = e.target.dataset; var Index = dataset.index; //通过`index`识别要删除第几条数据,第二个数据为要删除的项目数量,通常为1 this.data.list.splice(Index,1); //渲染数据 this.setData({ list:this.data.list }); }
Clear data
After adding, modifying or deleting, you have to clear the data again.
//清空 clear:function (){ //其实就是让数组变成一个空数组即可 this.setData({ list:{} }); }
Summary
Today we mainly talked about adding, modifying, deleting and clearing. In fact, there are many ways to operate arrays. You can see the following screenshots.
The above is the detailed content of WeChat applet learns array operations. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



What is PHP? PHP stands for Hypertext Preprocessor and is a popular server-side scripting language used for web development. It is designed to create dynamic and interactive web pages. PHP is embedded in HTML code and executed on the server, producing HTML output that is sent to the client browser. With its easy-to-learn syntax, PHP allows developers to build dynamic websites, process form data, interact with databases, and perform a variety of server-side tasks. It has a vast ecosystem of libraries and frameworks that enhance its functionality and enable developers to create powerful and scalable web applications. PHP is widely supported by hosting providers, making it a top choice for web development projects. How to put string into array and split by newline in PHP

PHP is a widely used server-side scripting language that can perform array operations in many different ways. This article will introduce our best practices when writing PHP code to help you create more efficient, beautiful, and readable code. 1. Use array functions instead of manual loops It is better to use PHP array functions instead of manually looping arrays to move, manipulate or modify data. PHP array functions execute faster and have better readability and maintainability. The following are some commonly used PHP array functions: array_push(

In PHP8.0 version, the array merging operation has been improved. This improvement mainly targets the merging operation of array data types. In previous versions, the array merging operations provided by PHP were implemented using the "+" symbol. However, there are some problems with this approach. If two arrays contain the same keys, the key values in the second array will overwrite the key values in the first array. If you need to merge the two arrays together, you need to use the array_merge() function skillfully. . Now, in PHP

Dangerous operations in arrays in PHP8.0: array_splice() In PHP programming, array is a very commonly used data structure that allows us to store multiple values in one variable. The array_splice() function is a method for processing arrays, which can delete or replace elements in the array. However, in PHP8.0, the array_splice() function has some dangerous operations, which if used improperly will cause some serious problems. This article will introduce you in detail

Array intersection and union functionality can be extended using PHP custom functions, custom intersection functions allow finding intersections by key or value, and custom union functions find unions by key or value. This gives you the flexibility to manipulate arrays based on your specific needs.

In PHP, array is a very common and useful data structure. PHP provides many different functions and methods to manipulate and process these arrays. One very useful function is array_diff(). This article discusses this function in detail. The basic usage of the array_diff() function is very simple. This function accepts two or more arrays as arguments and returns a new array containing elements that are present in the first array but not in the other arrays. Here is an example: $array1=

The best solution for PHP array key-value exchange: use the built-in array_flip() function, the time complexity is O(n). For larger arrays, the performance advantages of array_flip() are more obvious. Practical case: You can use array_flip() to convert the array of product names in the shopping cart into an array of product quantities.

How to add and remove elements from PHP arrays In PHP, arrays are a very common and important data structure. Arrays can hold multiple values and can dynamically add or subtract elements as needed. This article will explain how to add and remove array elements in PHP and provide corresponding code examples. 1. Add elements using square brackets [] syntax. The easiest way to add elements is to use square brackets [] syntax. An example is as follows: $arr=["apple",&quo
