JavaScript中字符串、數(shù)組和對象截取數(shù)據(jù)方法總結(jié)大全
數(shù)組截取數(shù)據(jù)方法
arr.slice(start, end)
start: 開始截取的索引(包含)
end: 結(jié)束截取的索引(不包含)
作用: 返回一個新數(shù)組,包含從開始到結(jié)束(不包括結(jié)束)的元素
const arr3 = [1, 2, 3, 4, 5]
const newArr = arr3.slice(1, 4)
console.log(newArr)// [2, 3, 4]arr.splice(start, deleteCount, item1, item2, ...)
start: 開始修改的索引
deleteCount: 要刪除的元素個數(shù)
item1, item2, ...: 要添加到數(shù)組的元素
作用: 刪除或替換數(shù)組中的元素,并返回被刪除的元素
const arr3 = [1, 2, 3, 4, 5]
const newArr = arr3.splice(1, 2)
console.log(newArr)// [2, 3]
console.log(arr3)//[1, 4, 5]array.filter(callback)
callback: 測試函數(shù),返回 true 或 false
作用: 創(chuàng)建一個新數(shù)組,包含通過測試函數(shù)的所有元素
const arr3 = [1, 2, 3, 4, 5]
const newArr = arr3.filter(item => item > 2)
console.log(newArr)// [3, 4, 5]
console.log(arr3)//[1, 2, 3, 4, 5]對象截取數(shù)據(jù)方法
對象解構(gòu)賦值 const { prop1, prop2 } = obj
作用: 從對象中提取屬性并賦值給變量
const obj = { a: 1, b: 2, c: 3 }
const { a, b } = obj
console.log(a) // 1
console.log(b) // 2Object.keys(obj)
作用: 返回一個包含對象所有可枚舉屬性的數(shù)組
返回值: 屬性名數(shù)組
const obj = { a: 1, b: 2, c: 3 }
const keys = Object.keys(obj) // ['a', 'b', 'c']
console.log(keys) Object.values(obj)
作用: 返回一個包含對象所有可枚舉屬性值的數(shù)組
返回值: 屬性值數(shù)組
const obj = { a: 1, b: 2, c: 3 }
const values = Object.values(obj)
console.log(values) //[1,2,3]Object.entries(obj)
作用: 返回一個包含對象所有可枚舉屬性的鍵值對數(shù)組
返回值: 鍵值對數(shù)組
const obj = { a: 1, b: 2, c: 3 }
const entries = Object.entries(obj) // [['a', 1], ['b', 2], ['c', 3]]
console.log(entries) Object.assign(target, ...sources)
作用: 將一個或多個源對象的屬性復制到目標對象
返回值: 目標對象
const obj1 = { a: 1 }
const obj2 = { b: 2 }
const newObj = Object.assign({}, obj1, obj2) // { a: 1, b: 2 }
console.log(newObj)字符串截取方法
str.slice(startIndex, endIndex)
startIndex: 開始截取的索引(包含)
endIndex: 結(jié)束截取的索引(不包含)。如果省略,則截取到字符串末尾
返回值: 新的字符串
作用: 提取字符串的一部分,并返回一個新的字符串
const str = "Hello, World!"
const newStr = str.slice(0, 5)// "Hello"
const newStr2 = str.slice(7) // "World!"
console.log(newStr)
console.log(newStr2)str.substring(startIndex, endIndex)
startIndex: 開始截取的索引(包含)
endIndex: 結(jié)束截取的索引(不包含)。如果省略,則截取到字符串末尾
注意:
如果 startIndex > endIndex,substring() 會自動交換兩個參數(shù)
不支持負數(shù)索引
返回值: 新的字符串
作用: 提取字符串的一部分,并返回一個新的字符串
const str = "Hello, World!"
const newStr = str.substring(0, 5)// Hello
const newStr2 = str.substring(7) // World!
console.log(newStr)
console.log(newStr2)str.substr(startIndex, length) 已棄用
startIndex: 開始截取的索引
length: 截取的長度。如果省略,則截取到字符串末尾
注意:
substr() 是非標準方法,已棄用,建議使用 slice() 或 substring()
返回值: 新的字符串
作用: 從指定位置開始截取指定長度的字符串
const str = "Hello, World!"
const newStr = str.substr(0, 5) // "Hello"
const newStr2 = str.substr(7) // "World!"
console.log(newStr)
console.log(newStr2)str.split(separator, limit)
separator: 分隔符(可以是字符串或正則表達式)
limit: 返回數(shù)組的最大長度
返回值: 拆分后的數(shù)組
作用: 將字符串按指定分隔符拆分為數(shù)組,然后可以截取部分內(nèi)容
const str = "Hello, World!"
const arr4 = str.split(" ") // ['Hello,', 'World!']
const firstWord = arr4[0] // Hello,
console.log(arr4)
console.log(firstWord)str.charAt(index)
index: 字符的索引
返回值: 指定位置的字符
作用: 返回字符串中指定位置的字符
const str = "Hello, World!"
const char = str.charAt(1)// "e"
console.log(char)str.charCodeAt(index)
index: 字符的索引
返回值: Unicode 編碼
作用: 返回字符串中指定位置的字符的 Unicode 編碼
const str = "Hello"
const code = str.charCodeAt(1) // 101
console.log(code)str.match(regexp)
regexp: 正則表達式
返回值: 匹配結(jié)果的數(shù)組,如果沒有匹配則返回 null
作用: 使用正則表達式匹配字符串,返回匹配結(jié)果
const str = "Hello, World!" const matches = str.match(/o/g) // ["o", "o"]
str.replace(searchValue, replaceValue)
searchValue: 要查找的內(nèi)容(可以是字符串或正則表達式)
replaceValue: 替換的內(nèi)容
返回值: 新的字符串
作用: 替換字符串中的部分內(nèi)容
const str = "Hello, World!"
const newStr = str.replace("World", "JavaScript") // "Hello, JavaScript!"str.trim()
返回值: 新的字符串
作用: 去除字符串兩端的空白字符
const str = " Hello, World! " const newStr = str.trim() // "Hello, World!"
str.padStart(targetLength, padString)
str.padEnd(targetLength, padString)
targetLength: 目標長度
padString: 填充的字符(默認為空格)
返回值: 新的字符串
作用: 在字符串的開頭或結(jié)尾填充指定字符,直到字符串達到指定長度
const str = "Hello" const paddedStart = str.padStart(10, "*") // "*****Hello" const paddedEnd = str.padEnd(10, "*") // "Hello*****"
總結(jié)
到此這篇關(guān)于JavaScript中字符串、數(shù)組和對象截取數(shù)據(jù)方法的文章就介紹到這了,更多相關(guān)js字符串、數(shù)組和對象截取方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談JavaScript構(gòu)造樹形結(jié)構(gòu)的一種高效算法
這篇文章主要介紹了JavaScript構(gòu)造樹形結(jié)構(gòu)的一種高效算法,對算法感興趣的同學,可以參考下2021-05-05
el-popover放在el-table中點擊無反應(yīng)問題解決方案
我們想在table中給btn加彈框但是?el-popover點擊按鈕沒有任何反應(yīng),解決思路是通過給每個el-popover都加上單獨的id,本文結(jié)合實例代碼給大家介紹的非常詳細,需要的朋友參考下吧2024-04-04

