


Commonly used JavaScript operations on the front end (code examples)
The content this article brings to you is about commonly used JavaScript operations (code examples) on the front end. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1. Delete a specified character in the string
Example: Delete km in "10km"
var str = "10km" //方法一: var res = str.replace('km', '') //方法二: var res = str.split('km').join('') //join方法不传参默认使用逗号作为分隔符
2. Array deduplication
var arr = [1, 2, 3, 1, 2] //方法一: var res = [...new Set(arr)] //方法二: var res = Array.from(new Set(arr)) //方法三: var res = []; for (var i in arr) { if (res.indexOf(arr[i] === -1) { res.push(arr[i]) } } //方法四: var res = [] arr.map((item, index) => { if (res.indexOf(item) === -1){ res.push(item) } })
Attachment: Usage of Array.from():
Array.from(arr, mapfn, thisArg): used to convert two types of objects into real arrays.
Array-like object (must have length attribute)
Traversable object (String with Iterator interface deployed, new in ES6 Map and Set).
Parameters: The first is an array, which must be passed; the second is a function (similar to the map function), which operates on the array elements and then returns the array, optional; the third The first is a pointer to the this keyword, optional.
var obj1 = { 0: 'a', 1: 'b', 2: 'c' } var arr1 = Array.from(obj1) console.log(arr1) // [] /* 1. 类数组对象,具有length属性,而普通对象是没有length属性的。*/ /* 2. 类数组对象的属性名必须为非负整数,对象中的属性名会被当做字符串处理。*/ var obj2 = { 0: 'a', 1: 'b', 2: 'c', length: 2 } var arr2 = Array.from(obj2) console.log(arr2) // ["a", "b"] var obj3 = { 0: 'a', 1: 'b', 2: 'c', length: 4 } var arr3 = Array.from(obj3) console.log(arr3) // ["a", "b", "c", undefined] var obj4 = { 0: 'a', 1: 'b', 2: 'c', length: 3 } var arr4 = Array.from(obj4, item => item + 1) console.log(arr4) // ["a1", "b1", "c1"] var obj5 = { "1": "a", "0": "b", length: 2 } var arr5 = Array.from(obj5) console.log(arr5) // ["b", "a"]
3. Convert pseudo-array object into array
var obj = { 0: 'a', 1: 'b', length: 2 } //方法一: Array.from(obj) //方法二: Array.prototype.slice.call(obj) //方法三: Array.prototype.concat.apply([], obj) //方法四: Array.prototype.splice.call(obj, 0) // 返回被删除的元素,原对象obj会被破坏掉 console.log(obj) // obj: {length: 0} // 上述的Array.prototype 均可用[]代替
4. Deep copy of array or object
//方法一: JSON.parse(JSON.stringify(obj)) //方法二:递归遍历 function clone (obj) { var res = obj.constructor === Array ? [] : {} for (var i in obj) { res[i] = typeof obj[i] === 'object' ? clone(obj[i]) : obj[i] // 即obj[i]为数组或对象,继续拷贝 } return res } //附:数组浅拷贝 var arr = ['a', ['b', ['c']]] //1.使用slice() var res = arr.slice(0) console.log(res) // ['a', ['b', ['c']]] res[1][1] = 'b' console.log(res) // ['a', ['b', ['b']]] console.log(arr) // ['a', ['b', ['b']]] //2.使用concat() var arr = ['a', ['b', ['c']]] var res = [].concat(arr) res[1][1] = 'b' console.log(res) // ['a', ['b', ['b']]] console.log(arr) // ['a', ['b', ['b']]] //Object.assign()也只能实现对象的浅拷贝,它只是一级属性复制,比浅拷贝多深拷贝了一层 var obj = {a: "a", b: {c: "d"}} var res = Object.assign({}, obj) res.b.c= "e" console.log(res) // {a: "a", b: {c: "e"}} console.log(obj) // {a: "a", b: {c: "e"}}
5. No block-level scope leads to inner variable coverage outer variables.
var date = new Date().getDate(); function f(){ console.log(date); if(false){ var date = 0;//变量提升 } } f();//undefined
6. Usage of tag templates in ES6:
let a = 1; let b = 2; function tag(arr, value1, value2){ console.log(arr); //["hello ", " world ", ""] console.log(value1); //3 console.log(value2); //2 } tag`hello ${a + b} world ${a * b}`; /** 如果函数名后的模板字符串中没有变量,则直接将其作为函数参数调用。 如果存在变量则先将模板字符串处理成多个参数,再调用函数。 处理规则: 1.默认该函数第一个参数为数组,该数组的成员是模板字符串中那些没有变量替换的部分。 2.变量替换只发生在数组的第一个成员与第二个成员之间、第二个成员与第三个成员之间。 以此类推,故arr中第三个成员为"",原因是${a * b}的变量替换发生在第二个成员与第三个成员之间, 所以必须存在第三个成员。 3.函数的其他参数,都是模板字符串各个变量被替换后的值。 函数形如: function tag(stringArr, ...value){} */
The above is the complete introduction, if you want to know more about HTML video tutorial, please pay attention to the PHP Chinese website.
The above is the detailed content of Commonly used JavaScript operations on the front end (code examples). 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

AI Hentai Generator
Generate AI Hentai for free.

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



The Node service built based on non-blocking and event-driven has the advantage of low memory consumption and is very suitable for handling massive network requests. Under the premise of massive requests, issues related to "memory control" need to be considered. 1. V8’s garbage collection mechanism and memory limitations Js is controlled by the garbage collection machine

Vue.js has become a very popular framework in front-end development today. As Vue.js continues to evolve, unit testing is becoming more and more important. Today we’ll explore how to write unit tests in Vue.js 3 and provide some best practices and common problems and solutions.

The file module is an encapsulation of underlying file operations, such as file reading/writing/opening/closing/delete adding, etc. The biggest feature of the file module is that all methods provide two versions of **synchronous** and **asynchronous**, with Methods with the sync suffix are all synchronization methods, and those without are all heterogeneous methods.

Cross-domain is a scenario often encountered in development, and it is also an issue often discussed in interviews. Mastering common cross-domain solutions and the principles behind them can not only improve our development efficiency, but also perform better in interviews.

PHP and Vue: a perfect pairing of front-end development tools. In today's era of rapid development of the Internet, front-end development has become increasingly important. As users have higher and higher requirements for the experience of websites and applications, front-end developers need to use more efficient and flexible tools to create responsive and interactive interfaces. As two important technologies in the field of front-end development, PHP and Vue.js can be regarded as perfect tools when paired together. This article will explore the combination of PHP and Vue, as well as detailed code examples to help readers better understand and apply these two

In front-end development interviews, common questions cover a wide range of topics, including HTML/CSS basics, JavaScript basics, frameworks and libraries, project experience, algorithms and data structures, performance optimization, cross-domain requests, front-end engineering, design patterns, and new technologies and trends. . Interviewer questions are designed to assess the candidate's technical skills, project experience, and understanding of industry trends. Therefore, candidates should be fully prepared in these areas to demonstrate their abilities and expertise.

With the development of Internet technology, front-end development has become increasingly important. Especially the popularity of mobile devices requires front-end development technology that is efficient, stable, safe and easy to maintain. As a rapidly developing programming language, Go language has been used by more and more developers. So, is it feasible to use Go language for front-end development? Next, this article will explain in detail how to use Go language for front-end development. Let’s first take a look at why Go language is used for front-end development. Many people think that Go language is a

At the beginning, JS only ran on the browser side. It was easy to process Unicode-encoded strings, but it was difficult to process binary and non-Unicode-encoded strings. And binary is the lowest level data format of the computer, video/audio/program/network package
