成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

JS數(shù)據(jù)結(jié)構(gòu)0x001:數(shù)組

Rindia / 927人閱讀

摘要:概述這里講的數(shù)組是指數(shù)據(jù)結(jié)構(gòu)中的數(shù)組,而不是專指中的數(shù)組,只是使用來探究數(shù)據(jù)結(jié)構(gòu)中的數(shù)組,因為我覺得比較方便。

0x000 概述

這里講的數(shù)組是指數(shù)據(jù)結(jié)構(gòu)中的數(shù)組,而不是專指js中的數(shù)組,只是使用js來探究數(shù)據(jù)結(jié)構(gòu)中的數(shù)組,因為我覺得js比較方便。

0x001 數(shù)組

數(shù)組是啥?看圖:

數(shù)組有兩個要素:

索引:圖中的0,1,2,3,4

數(shù)據(jù):圖中的data1-data5
可以通過索引找到某個數(shù)據(jù),然后對這個數(shù)據(jù)操作,而這里的數(shù)據(jù)是泛指,因為數(shù)組是一種通用的數(shù)據(jù)結(jié)構(gòu),可以存儲任意的數(shù)據(jù),比如數(shù)字、對象、字符串,甚至數(shù)組也可以。

數(shù)組的操作

搜索

添加/更新

刪除

0x002 初始化

js中數(shù)組的初始化很簡單,方式也很多:

[] // []
[1,2,3,4,5,6,7] //[1,2,3,4,5,6,7]
new Array() //[]
new Array(10) // [ 10 empty items]
new Array(1,2,3,4,6) // [1,2,3,4,5,6]
Array.from([1,2,3,4,5],(a)=>a*2) // [2,4,6,8,10]
Array.from(new Set([1,2,3,3])) // [1,2,3]

這里我們選擇最簡單的來實現(xiàn)init

function init() {
    return []
}
0x002 插入

js中插入的方法也很多,每個方法也都有自己的特色,其實js的數(shù)組就已經(jīng)自帶實現(xiàn)了很多的數(shù)據(jù)結(jié)構(gòu)

let data=[]
data[0]=1 // [1]
data.push(2) // [1, 2]
data=data.concat(3) // [1, 2, 3]
data=data.concat([4],5) // [1, 2, 3, 4, 5]

我們依舊選擇最簡單索引方式,因為這比較符合數(shù)據(jù)結(jié)構(gòu)中數(shù)組的使用,push是更適合其他數(shù)據(jù)結(jié)構(gòu)的操作。

function insert(arr, index, data) {
    arr[index] = data
    return arr
}
0x003 查找

js數(shù)組查找的方法也很多

let data = [1, 2, 3, 4, 5, 6]
data.find(d => d === 1) // 1
data[data.findIndex(d => d === 2)] //2
data.filter(d => d === 3)[0] // 3
data.forEach(d => {
    if (d === 4) {
        result = d // 4
    }
})  
for (let i = 0; i < data.length; i++) {
    if (data[i] === 5) {
        result = data[i] // 5
        break
    }
} 

for (var d of data) {
    if (d === 6) {
        result = d //6
        break
    }
}

我們依舊采用最簡單的

function find(arr, data) {
    return arr.find(d => d === data)
}
0x004 刪除

js的刪除...也有很多方法

let data = [1, 2, 3, 4, 5, 6] 
delete data[0] // [ <1 empty item>, 2, 3, 4, 5, 6 ]
data.pop() // [ <1 empty item>, 2, 3, 4, 5]
data.splice(0, 1) // [2, 3, 4, 5]

我們依舊采用最簡單的

function delete_(arr, index) {
    arr.splice(index,1)
    return arr
}
0x005 使用
function main() {
    let arr = init()
    
    arr = insert(arr, 0, 1) // [1]
    arr = insert(arr, 1, 2) // [1, 2]
    arr = insert(arr, 2, 3) // [1, 2, 3]
    arr = insert(arr, 3, 4) // [1, 2, 3, 4]
    arr = insert(arr, 4, 5) // [1, 2, 3, 4, 5]
    arr = insert(arr, 5, 6) // [1, 2, 3, 4, 5, 6]
    
    find(arr, 1) // 1
    find(arr, 2) // 2
    find(arr, 3) // 3
    find(arr, 4) // 4
    find(arr, 5) // 5 
    
    delete_(arr, 0)
    delete_(arr, 1)
    delete_(arr, 2)
    delete_(arr, 3)
    delete_(arr, 4)
    delete_(arr, 5)
}
0x006 注意

當(dāng)然,我們平常并不會這么使用js,這只是為了演示數(shù)組而已:

let data=[1,2,3]
data.push(4)
data.push(5)
data.push(6)
data.filter(d=>d===1)
data.splice(0,1)
0x007 栗子:使用數(shù)組完成todoList

效果

todoService: 該文件用來提供todo的增刪改查服務(wù)

let todoService = []

/**
 * 獲取所有的 todo

 */
function getAll() {
    return todoService
}

/**
 * 添加一個 todo 到 todo 列表中
 * @param todo
 */
function add(todo) {
    todo.id = todoService.length
    todoService.push(todo)
}

/**
 * 根據(jù) todo 的 id 刪除一個 todo
 * @param id
 * @private
 */
function delete_(id) {
    todoService.splice(findIndexById(id), 1)
}

/**
 * 根據(jù)一個修改過的 todo 更新 todo
 * @param todo
 */
function update(todo) {
    todoService[findIndexById(todo.id)] = {...todo}
}

/**
 * 根據(jù)內(nèi)容篩選符合條件的 todo
 * @param content
 * @returns {*[]}
 */
function find(content) {
    return todoService.filter(todo => todo.content === content)
}

/**
 * 根據(jù) id 獲取這個 id 在 todoList 中的索引
 * @param id
 * @returns {number}
 */
function findIndexById(id) {
    return todoService.findIndex(todo => todo.id === +id)
}

```

視圖


    

引入todoService

初始化變量

        let $btnAdd = window.document.getElementById("btnAdd")
        let $btnSearch = window.document.getElementById("btnSearch")
        let $btnUpdate = window.document.getElementById("btnUpdate")
        let $ulTodoList = window.document.getElementById("ulTodoList")
        let $inputContent = window.document.getElementById("inputContent")
        let updateTodo

完成添加按鈕的點(diǎn)擊事件

當(dāng)用戶輸入內(nèi)容并點(diǎn)擊添加的時候,會根據(jù)輸入內(nèi)容創(chuàng)建一個新的todo,并調(diào)用add將新的todo保存到todoList中,接著調(diào)用render將所有的todo渲染到dom中,最后清空輸入框。
       $btnAdd.onclick = () => {
            let content = $inputContent.value
            add({content: content})
            render([...getAll()])
            $inputContent.value = ""
        }

完成搜索按鈕點(diǎn)擊事件

當(dāng)用戶輸入內(nèi)容并點(diǎn)擊搜索按鈕的時候,會根據(jù)輸入的內(nèi)容調(diào)用find,該函數(shù)返回了所有內(nèi)容和輸入內(nèi)容相同的todo,將這些todo渲染到dom中就獲得了搜索之后的todo,最后清空輸入框。
$btnSearch.onclick = () => {
            let content = $inputContent.value
            render(find(content))
            $inputContent.value = ""
        }

完成todoLsit的渲染

為了方便,該函數(shù)直接將ul的子元素全部清空,然后根據(jù)傳入的todoList重新渲染子元素,其中為每個一個todo創(chuàng)建了一個刪除按鈕和更新按鈕。刪除按鈕點(diǎn)擊的時候講調(diào)用delete_將這個 todotodoList中移除,更新按鈕點(diǎn)擊的時候只會將當(dāng)前的todo保存到變量中,準(zhǔn)備進(jìn)行更新操作
function render(todoList) {
            $ulTodoList.innerHTML = ""
            todoList.map((todo) => {
                let $li = document.createElement("li")
                $li.className = "mt-2"
    
                let $span = document.createElement("span")
                $span.innerText = todo.content
    
                let $btnDelete = document.createElement("button")
                $btnDelete.innerText = "刪除"
                $btnDelete.className = "btn btn-danger m-2"
                $btnDelete.onclick = () => {
                    delete_(todo.id)
                    render([...getAll()])
                }
    
                let $btnUpdate = document.createElement("button")
                $btnUpdate.innerText = "更新"
                $btnUpdate.className = "btn btn-warning m-2"
                $btnUpdate.onclick = () => {
                    updateTodo = {...todo}
                    $inputContent.value = todo.content
                }
    
                $li.appendChild($btnDelete)
                $li.appendChild($btnUpdate)
                $li.appendChild($span)
                $ulTodoList.appendChild($li)
            })
        }

完成更新按鈕點(diǎn)擊事件

render更新按鈕點(diǎn)擊的時候已經(jīng)將要更新的todo保存到updateTodo中,當(dāng)用戶修改輸入框內(nèi)容并點(diǎn)擊更新的時候,就會將舊的todo和新的todo合并成更新后的todo,然后調(diào)用update去更新這個todo,接著再render一次,最后清空輸入框
 $btnUpdate.onclick = () => {
            update({...updateTodo, content: $inputContent.value})
            render([...getAll()])
            $inputContent.value = ""
        }

0x007 資源

源代碼:https://github.com/followWinter/data-structure

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/98937.html

相關(guān)文章

  • React入門0x001: 環(huán)境配置和 helloworld

    摘要:概述開坑系列文章,不知道會寫到什么程度,畢竟寫文章并不在行,存在當(dāng)做筆記做,先不講理論,實踐先行。 0x000 概述 開坑 react 系列文章,不知道會寫到什么程度,畢竟寫文章并不在行,存在當(dāng)做筆記做,先不講理論,實踐先行。 0x001 創(chuàng)建項目 $ mkdir 0x001-helloworld $ cd 0x001-helloworld $ yarn init -y 0x0002 ...

    yibinnn 評論0 收藏0
  • JS數(shù)據(jù)結(jié)構(gòu)0x004:鏈表

    摘要:概述這篇文章是說鏈表,鏈表這種數(shù)據(jù)結(jié)構(gòu)非常普遍,有時候我們根本就沒有意識到用的是鏈表啥是鏈表鏈表就是用繩子連起來的酒瓶子,酒就是數(shù)據(jù),每個酒瓶子都連著下一個酒瓶子。 0x000 概述 這篇文章是說鏈表,鏈表這種數(shù)據(jù)結(jié)構(gòu)非常普遍,有時候我們根本就沒有意識到用的是鏈表 0x001 啥是鏈表 鏈表就是用繩子連起來的酒瓶子,酒就是數(shù)據(jù),每個酒瓶子都連著下一個酒瓶子。 showImg(https...

    sumory 評論0 收藏0
  • es6基礎(chǔ)0x001:箭頭函數(shù)

    摘要:這就是所謂的箭頭函數(shù)不綁定,而在我看來,回調(diào)函數(shù)就是箭頭函數(shù)最好的歸宿。 0x000 概述 箭頭函數(shù)有兩個作用: 更簡短的寫法 不綁定this 0x001 語法一表覽 ()=>{} ()=>{console.log(arrow);return null} ()=>hello (num1, num2)=>num1+num2 num=>++num ()=>({name:arrow})...

    stonezhu 評論0 收藏0
  • JS數(shù)據(jù)結(jié)構(gòu)0x003:隊列

    0x000 概述 這篇文章說的是隊列,隊列的用處也賊大,削峰、限流、消息異步化等等等 0x001 什么是隊列 隊列就是先入先出的數(shù)組,就和平常銀行排隊一樣,先排隊的人先處理事務(wù),如圖 showImg(https://segmentfault.com/img/bVbi4Hp?w=1774&h=560);只有兩個操作: 入隊:將數(shù)據(jù)放入隊列 出隊:將數(shù)據(jù)取出并處理 0x002 初始化 js中的隊列...

    xuhong 評論0 收藏0
  • JS數(shù)據(jù)結(jié)構(gòu)0x002:棧

    摘要:概述今天玩得是棧,棧的用處非常廣泛啊,比如函數(shù)的調(diào)用棧啊,的的的啊,之類的,一坨一坨的。 0x000 概述 今天玩得是棧,棧的用處非常廣泛啊,比如函數(shù)的調(diào)用棧啊,h5的history的state的api啊,之類的,一坨一坨的。 0x001 什么是棧 棧就是一個后入先出的數(shù)組,并且這個數(shù)組只能從一端進(jìn)來,再從這一端出去,就像是放在長筒紙盒里面的羽毛球,他只有兩個動作 push: 將數(shù)...

    noONE 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<