最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript中字符串、數(shù)組和對(duì)象截取數(shù)據(jù)方法總結(jié)大全

 更新時(shí)間:2025年05月27日 09:26:20   作者:lryh_  
這篇文章主要介紹了JavaScript中字符串、數(shù)組和對(duì)象截取數(shù)據(jù)方法的相關(guān)資料,需要的朋友可以參考下

數(shù)組截取數(shù)據(jù)方法

arr.slice(start, end)

start: 開(kāi)始截取的索引(包含)

end: 結(jié)束截取的索引(不包含)

作用: 返回一個(gè)新數(shù)組,包含從開(kāi)始到結(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: 開(kāi)始修改的索引

deleteCount: 要?jiǎng)h除的元素個(gè)數(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: 測(cè)試函數(shù),返回 true 或 false

作用: 創(chuàng)建一個(gè)新數(shù)組,包含通過(guò)測(cè)試函數(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]

對(duì)象截取數(shù)據(jù)方法

對(duì)象解構(gòu)賦值 const { prop1, prop2 } = obj

作用: 從對(duì)象中提取屬性并賦值給變量

        const obj = { a: 1, b: 2, c: 3 }
        const { a, b } = obj
        console.log(a) // 1
        console.log(b) // 2

Object.keys(obj)

作用: 返回一個(gè)包含對(duì)象所有可枚舉屬性的數(shù)組

返回值: 屬性名數(shù)組

        const obj = { a: 1, b: 2, c: 3 }
        const keys = Object.keys(obj) // ['a', 'b', 'c']
        console.log(keys) 

Object.values(obj)

作用: 返回一個(gè)包含對(duì)象所有可枚舉屬性值的數(shù)組

返回值: 屬性值數(shù)組

        const obj = { a: 1, b: 2, c: 3 }
        const values = Object.values(obj) 
        console.log(values) //[1,2,3]

 Object.entries(obj)

作用: 返回一個(gè)包含對(duì)象所有可枚舉屬性的鍵值對(duì)數(shù)組

返回值: 鍵值對(duì)數(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)

作用: 將一個(gè)或多個(gè)源對(duì)象的屬性復(fù)制到目標(biāo)對(duì)象

返回值: 目標(biāo)對(duì)象

        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: 開(kāi)始截取的索引(包含)

endIndex: 結(jié)束截取的索引(不包含)。如果省略,則截取到字符串末尾

返回值: 新的字符串

作用: 提取字符串的一部分,并返回一個(gè)新的字符串

        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: 開(kāi)始截取的索引(包含)

endIndex: 結(jié)束截取的索引(不包含)。如果省略,則截取到字符串末尾

注意:

如果 startIndex > endIndex,substring() 會(huì)自動(dòng)交換兩個(gè)參數(shù)

不支持負(fù)數(shù)索引

返回值: 新的字符串

作用: 提取字符串的一部分,并返回一個(gè)新的字符串

        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: 開(kāi)始截取的索引

length: 截取的長(zhǎng)度。如果省略,則截取到字符串末尾

注意:

substr() 是非標(biāo)準(zhǔn)方法,已棄用,建議使用 slice() 或 substring()

返回值: 新的字符串

作用: 從指定位置開(kāi)始截取指定長(zhǎng)度的字符串

        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: 分隔符(可以是字符串或正則表達(dá)式)

limit: 返回?cái)?shù)組的最大長(zhǎng)度

返回值: 拆分后的數(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: 正則表達(dá)式

返回值: 匹配結(jié)果的數(shù)組,如果沒(méi)有匹配則返回 null

作用: 使用正則表達(dá)式匹配字符串,返回匹配結(jié)果

const str = "Hello, World!"
const matches = str.match(/o/g) // ["o", "o"]

str.replace(searchValue, replaceValue)

searchValue: 要查找的內(nèi)容(可以是字符串或正則表達(dá)式)

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: 目標(biāo)長(zhǎng)度

padString: 填充的字符(默認(rèn)為空格)

返回值: 新的字符串

作用: 在字符串的開(kāi)頭或結(jié)尾填充指定字符,直到字符串達(dá)到指定長(zhǎng)度

const str = "Hello"
const paddedStart = str.padStart(10, "*") // "*****Hello"
const paddedEnd = str.padEnd(10, "*") // "Hello*****"

總結(jié) 

到此這篇關(guān)于JavaScript中字符串、數(shù)組和對(duì)象截取數(shù)據(jù)方法的文章就介紹到這了,更多相關(guān)js字符串、數(shù)組和對(duì)象截取方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

平舆县| 承德市| 普宁市| 乐山市| 土默特左旗| 丹寨县| 溧水县| 陕西省| 武强县| 宿州市| 通城县| 广安市| 伊通| 沐川县| 沙河市| 凌源市| 肇源县| 潍坊市| 宿迁市| 鸡西市| 阜平县| 珲春市| 莆田市| 商城县| 宣恩县| 夏河县| 西乡县| 永胜县| 桑植县| 罗田县| 洛阳市| 姜堰市| 北宁市| 图片| 县级市| 上栗县| 海原县| 即墨市| 合水县| 北流市| 阿拉善盟|