国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

js樹形結(jié)構(gòu)數(shù)組扁平化

這篇具有很好參考價(jià)值的文章主要介紹了js樹形結(jié)構(gòu)數(shù)組扁平化。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

js樹形結(jié)構(gòu)數(shù)組扁平化 1. 樹形結(jié)構(gòu) ---- > 扁平化數(shù)據(jù)

一、樹形結(jié)構(gòu) ---- > 扁平化數(shù)據(jù) (數(shù)據(jù))

const newData: any = [
		{
			"id": "1",
			"pId": null,
			"title": "長(zhǎng)期",
			"ywid": true,
			"children": [
				{
					"id": "3",
					"pId": "2",
					"title": "短期",
					"children": [],
					"origin": null,
					"parentId": "2",
					"projectId": "1BD75301A441F0419098AEFA0129A9B4",
					"state": "0",
					"name": "長(zhǎng)貸",
					"financeType": null,
					"shareholderName": "魅力公司",
					"shareholderType": "0",
					"ownershipRatio": null,
					"expectedTotalAmount": 1000,
					"planTotalAmount": 900,
					"remark": "備注",
					"isAllowDel": "0",
					"orderNo": "1.1",
					"value": "3",
					"key": "3",
					"pid": "2",
					"isLeaf": false
				},
				{
					"id": "4",
					"pId": "2",
					"title": "短期",
					"children": [],
					"origin": null,
					"parentId": "2",
					"projectId": "1BD75301A441F0419098AEFA0129A9B4",
					"state": "0",
					"name": "短期",
					"financeType": null,
					"shareholderName": "魅力公司",
					"shareholderType": "0",
					"ownershipRatio": null,
					"expectedTotalAmount": 1000,
					"planTotalAmount": 900,
					"remark": "備注",
					"isAllowDel": "0",
					"orderNo": "1.2",
					"value": "4",
					"key": "4",
					"pid": "2",
					"isLeaf": false
				}
			],
			"origin": null,
			"parentId": null,
			"projectId": "1BD75301A441F0419098AEFA0129A9B4",
			"state": "0",
			"name": "金期",
			"financeType": null,
			"shareholderName": "魅力公司",
			"shareholderType": "0",
			"ownershipRatio": null,
			"expectedTotalAmount": 1000,
			"planTotalAmount": 900,
			"remark": "備注",
			"isAllowDel": "0",
			"orderNo": "1",
			"value": "1",
			"key": "1",
			"pid": null,
			"isLeaf": true,

		},
		{
			"id": "2",
			"pId": null,
			"title": "國(guó)金",
			"ywid": true,
			"children": [
				{
					"id": "3",
					"pId": "2",
					"title": "長(zhǎng)期",
					"children": [],
					"origin": null,
					"parentId": "2",
					"projectId": "1BD75301A441F0419098AEFA0129A9B4",
					"state": "0",
					"name": "長(zhǎng)期",
					"financeType": null,
					"shareholderName": "魅力公司",
					"shareholderType": "0",
					"ownershipRatio": null,
					"expectedTotalAmount": 1000,
					"planTotalAmount": 900,
					"remark": "備注",
					"isAllowDel": "0",
					"orderNo": "2.1",
					"value": "3",
					"key": "3",
					"pid": "2",
					"isLeaf": false
				},
				{
					"id": "4",
					"pId": "2",
					"title": "短期",
					"children": [],
					"origin": null,
					"parentId": "2",
					"projectId": "1BD75301A441F0419098AEFA0129A9B4",
					"state": "0",
					"name": "短期",
					"financeType": null,
					"shareholderName": "魅力公司",
					"shareholderType": "0",
					"ownershipRatio": null,
					"expectedTotalAmount": 1000,
					"planTotalAmount": 900,
					"remark": "備注",
					"isAllowDel": "0",
					"orderNo": "2.2",
					"value": "4",
					"key": "4",
					"pid": "2",
					"isLeaf": false
				}
			],
			"origin": null,
			"parentId": null,
			"projectId": "1BD75301A441F0419098AEFA0129A9B4",
			"state": "0",
			"name": "國(guó)金",
			"financeType": null,
			"shareholderName": "魅力公司",
			"shareholderType": "0",
			"ownershipRatio": null,
			"expectedTotalAmount": 1000,
			"planTotalAmount": 900,
			"remark": "備注",
			"isAllowDel": "0",
			"orderNo": "2",
			"value": "2",
			"key": "2",
			"pid": null,
			"isLeaf": false
		},
		{
			"id": "5",
			"pId": null,
			"title": "利用金",
			"ywid": true,
			"children": [
				{
					"id": "3",
					"pId": "2",
					"title": "長(zhǎng)期",
					"children": [],
					"origin": null,
					"parentId": "2",
					"projectId": "1BD75301A441F0419098AEFA0129A9B4",
					"state": "0",
					"name": "長(zhǎng)期",
					"financeType": null,
					"shareholderName": "魅力公司",
					"shareholderType": "0",
					"ownershipRatio": null,
					"expectedTotalAmount": 1000,
					"planTotalAmount": 900,
					"remark": "備注",
					"isAllowDel": "0",
					"orderNo": "2.1",
					"value": "3",
					"key": "3",
					"pid": "2",
					"isLeaf": false
				}
			],
			"origin": null,
			"parentId": null,
			"projectId": "1BD75301A441F0419098AEFA0129A9B4",
			"state": "0",
			"name": "利用金",
			"financeType": null,
			"shareholderName": "魅力公司",
			"shareholderType": "0",
			"ownershipRatio": null,
			"expectedTotalAmount": 1000,
			"planTotalAmount": 900,
			"remark": "備注",
			"isAllowDel": "0",
			"orderNo": "3",
			"value": "5",
			"key": "5",
			"pid": null,
			"isLeaf": true
		},
		{
			"id": "6",
			"pId": null,
			"title": "其他金",
			"ywid": true,
			"children": [],
			"origin": null,
			"parentId": null,
			"projectId": "1BD75301A441F0419098AEFA0129A9B4",
			"state": "0",
			"name": "其他金",
			"financeType": null,
			"shareholderName": "魅力公司",
			"shareholderType": "0",
			"ownershipRatio": null,
			"expectedTotalAmount": 1000,
			"planTotalAmount": 900,
			"remark": "備注",
			"isAllowDel": "0",
			"orderNo": "4",
			"value": "6",
			"key": "6",
			"pid": null,
			"isLeaf": true
		}
	]

二、代碼如下

1.引入庫(kù)

1.代碼如下(數(shù)據(jù)扁平化1示例):

/**
 * 獲取當(dāng)前節(jié)點(diǎn)的所有父節(jié)點(diǎn)
 * @param list 
 * @param current 
 * @returns 
 */

const findParentId = (list, current) => {
    const res:string[] = [];
    function find(list, id) {
        list.forEach(item => {
            if (item.key === id) {
                res.unshift(id)
                if (item.parentId) {
                    find(list, item.parentId)
                }
            }
        });
    }
    find(list, current)
    return res
}

	/**
 * 數(shù)據(jù)扁平化
 * @param list 
 * @returns 
 */
	const flatTreeData = (list) => {
		const res: any = []
		function getData(list) {
			list.forEach(item => {
				res.push(item);
				if (item.children) {
					getData(item.children)
				}
			})
		}
		getData(list)
		return res
	}

	const a = flatTreeData(newData)
	console.log(a,"數(shù)據(jù)扁平化1")
/**
 * 將選中節(jié)點(diǎn)的父節(jié)點(diǎn)合并到選中節(jié)點(diǎn)中去
 * @param list 
 * @param selected 
 * @returns 
 */
const handleSelectedData = (list, selected)=>{
    const res:any[] = []
    const data= flatTreeData(list)
    selected.forEach(id=>{
        const items = findParentId(data,id)
       
        res.push(...items)
    })
    return Array.from(new Set(res))
}

2.代碼如下(數(shù)據(jù)扁平化2示例):

	/**
 * 數(shù)據(jù)扁平化
 * @param source 
 * @returns 
 */
	const fn = (source)=>{
		let res:any = []
		source.forEach(el=>{
			res.push(el)
			el.children && res.push(...fn(el.children))
		})
		return res
	}
	console.log(fn(newData),"數(shù)據(jù)扁平化2")

3.代碼如下(數(shù)據(jù)扁平化3示例):

	/**
 * 數(shù)據(jù)扁平化
 * @param arr 
 * @returns 
 */
//用棧實(shí)現(xiàn)(非遞歸)
	function flapvalue(arr) {
		let stack: any = []
		let newArr: any = []   // 扁平結(jié)構(gòu)的數(shù)組
		for (let i = arr.length - 1; i >= 0; i--) {  // 倒序遍歷數(shù)組,push到棧中
			stack.push(arr[i])
		}
		while (stack.length) {
			let item = stack.pop()
			newArr.push(item)
			if (item.children) {
				let children = item.children
				for (let i = children.length - 1; i >= 0; i--) {
					stack.push(children[i])
				}
			}
		}
		return newArr
	}
	console.log(flapvalue(newData),'數(shù)據(jù)扁平化3')

4.代碼如下(數(shù)據(jù)扁平化4示例):文章來源地址http://www.zghlxwxcb.cn/news/detail-515504.html

	/**
 * 數(shù)據(jù)扁平化
 * @param tree
 * @returns 
 */
	function treeToArray(tree) {
		var res:any = []
		for (const item of tree) {
		  const { children, ...i } = item
		  if (children && children.length) {
			res = res.concat(treeToArray(children))
		  }
		  res.push(i)
		}
		return res
	  }
	  console.log(treeToArray(newData),'數(shù)據(jù)扁平化4')

到了這里,關(guān)于js樹形結(jié)構(gòu)數(shù)組扁平化的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 022:vue中tree結(jié)構(gòu)數(shù)據(jù)變成扁平化table結(jié)構(gòu)數(shù)據(jù)的示例

    022:vue中tree結(jié)構(gòu)數(shù)據(jù)變成扁平化table結(jié)構(gòu)數(shù)據(jù)的示例

    第022個(gè) 查看專欄目錄: VUE ------ element UI 在vue和element UI聯(lián)合技術(shù)棧的操控下,本專欄提供行之有效的源代碼示例和信息點(diǎn)介紹,做到靈活運(yùn)用。 (1)提供vue2的一些基本操作:安裝、引用,模板使用,computed,watch,生命周期(beforeCreate,created,beforeMount,mounted, beforeUpdate,upda

    2024年02月12日
    瀏覽(24)
  • 5個(gè)常見的前端手寫功能:淺拷貝與深拷貝、函數(shù)柯里化、數(shù)組扁平化、數(shù)組去重、手寫類型判斷函數(shù)

    5個(gè)常見的前端手寫功能:淺拷貝與深拷貝、函數(shù)柯里化、數(shù)組扁平化、數(shù)組去重、手寫類型判斷函數(shù)

    淺拷貝 淺拷貝是創(chuàng)建一個(gè)新對(duì)象,這個(gè)對(duì)象有著原始對(duì)象屬性值的一份精確拷貝。如果屬性是基本類型,拷貝的就是基本類型的值,如果屬性是引用類型,拷貝的就是內(nèi)存地址,所以如果其中一個(gè)對(duì)象改變了這個(gè)地址,就會(huì)影響到另一個(gè)對(duì)象。 測(cè)試結(jié)果: 深拷貝 深拷貝是將

    2024年04月26日
    瀏覽(22)
  • 深度學(xué)習(xí)中Numpy的一些注意點(diǎn)(多維數(shù)組;數(shù)據(jù)類型轉(zhuǎn)換、數(shù)組扁平化、np.where()、np.argmax()、圖像拼接、生成同shape的圖片)

    深度學(xué)習(xí)中Numpy的一些注意點(diǎn)(多維數(shù)組;數(shù)據(jù)類型轉(zhuǎn)換、數(shù)組扁平化、np.where()、np.argmax()、圖像拼接、生成同shape的圖片)

    a.shape=(3,2);既數(shù)組h=3,w=2 a.shape=(2,3,2);這里第一個(gè)2表示axis=0維度上的,三維數(shù)組中3,2)數(shù)組的個(gè)數(shù),這里表示兩個(gè)(3,2)數(shù)組。 這里axis=0指代哪里是很重要的知識(shí)點(diǎn)。深度學(xué)習(xí)中經(jīng)常壓縮一個(gè)維度,axis=0。 numpy.squeeze()函數(shù)。 語法:numpy.squeeze(a,axis = None);作用是將shape維度為

    2024年01月18日
    瀏覽(42)
  • Golang每日一練(leetDay0118) 扁平化嵌套列表迭代器、整數(shù)拆分

    Golang每日一練(leetDay0118) 扁平化嵌套列表迭代器、整數(shù)拆分

    目錄 341. 扁平化嵌套列表迭代器 Flatten Nested List Iterator?????? 343. 整數(shù)拆分 Integer Break?????? ?? 每日一練刷題專欄??? Rust每日一練 專欄 Golang每日一練 專欄 Python每日一練 專欄 C/C++每日一練 專欄 Java每日一練 專欄 給你一個(gè)嵌套的整數(shù)列表? nestedList ?。每個(gè)元素要么是

    2024年02月16日
    瀏覽(53)
  • js處理扁平數(shù)組和樹結(jié)構(gòu)相互轉(zhuǎn)換

    一、將扁平的數(shù)據(jù)轉(zhuǎn)為樹形結(jié)構(gòu) 在 js中,可以使用遞歸算法將扁平的數(shù)據(jù)轉(zhuǎn)換為樹形結(jié)構(gòu)。 扁平數(shù)據(jù)通常是一個(gè)帶有 parentId 屬性的數(shù)組,而樹形結(jié)構(gòu)通常是一個(gè)帶有 children 屬性的對(duì)象。 1、方法一 下面是一個(gè)簡(jiǎn)單的例子,演示如何將扁平數(shù)組轉(zhuǎn)換為樹形對(duì)象: 2、方法二

    2024年02月12日
    瀏覽(17)
  • js遞歸遍歷樹形結(jié)構(gòu)數(shù)據(jù),獲取所有數(shù)組id集合

    實(shí)現(xiàn)思路 可以使用遞歸遍歷整個(gè)樹形數(shù)組,將每個(gè)節(jié)點(diǎn)的id加入到一個(gè)數(shù)組中,最后返回這個(gè)數(shù)組即可。 數(shù)據(jù)準(zhǔn)備 代碼實(shí)現(xiàn) 方式一 獲取結(jié)果 方式二 獲取結(jié)果 方式三 獲取結(jié)果 方法總結(jié) 這里的tree是樹形數(shù)組,result是用來保存所有id的數(shù)組。 首先遍歷當(dāng)前層級(jí)的每個(gè)節(jié)點(diǎn),

    2024年02月11日
    瀏覽(22)
  • "樹形List"與"扁平List"互轉(zhuǎn)(Java實(shí)現(xiàn))

    背景: 在平時(shí)的開發(fā)中,我們時(shí)常會(huì)遇到下列場(chǎng)景 公司的 組織架構(gòu) 的數(shù)據(jù)存儲(chǔ)與展示 文件夾 層級(jí) 的數(shù)據(jù)存儲(chǔ)與展示 評(píng)論系統(tǒng)中, 父評(píng)論與諸多子評(píng)論 的數(shù)據(jù)存儲(chǔ)與展示 ...... 對(duì)于這種有層級(jí)的結(jié)構(gòu)化數(shù)據(jù),就像是一棵 樹 一樣。在關(guān)系型數(shù)據(jù)庫(kù)中,通常將一個(gè)個(gè)的節(jié)點(diǎn)

    2024年02月03日
    瀏覽(22)
  • js遞歸操作樹形結(jié)構(gòu)

    js遞歸操作樹形結(jié)構(gòu)

    數(shù)據(jù)結(jié)構(gòu) 轉(zhuǎn)成扁平化 運(yùn)行結(jié)果 扁平結(jié)構(gòu)轉(zhuǎn)成樹形結(jié)構(gòu) 數(shù)據(jù)結(jié)構(gòu) 轉(zhuǎn)成樹形結(jié)構(gòu) 運(yùn)行結(jié)果 數(shù)據(jù)結(jié)構(gòu) 根據(jù)parentId查找父節(jié)點(diǎn) 運(yùn)行結(jié)果(會(huì)把自己及父節(jié)點(diǎn)保存下來) 需求:根據(jù)點(diǎn)擊子節(jié)點(diǎn)查詢沒有就一直向上查 思路:拿到樹形結(jié)構(gòu)數(shù)組直接遞歸查詢 難點(diǎn): ? ? ? ? 1)獲取

    2024年02月04日
    瀏覽(23)
  • 微信小程序多列下拉框的實(shí)現(xiàn)(樹形數(shù)據(jù)結(jié)構(gòu)和單數(shù)組數(shù)據(jù)結(jié)構(gòu)形式)

    微信小程序多列下拉框的實(shí)現(xiàn)(樹形數(shù)據(jù)結(jié)構(gòu)和單數(shù)組數(shù)據(jù)結(jié)構(gòu)形式)

    利用微信小程序api,實(shí)現(xiàn)不同傳輸數(shù)據(jù)格式下的多列下拉框?qū)崿F(xiàn) 首先了解一下picker中的事件 這里是官方文檔,具體意思就是 當(dāng)你滑動(dòng)多列中的某一列的時(shí)候, bindcolumnchange 事件就會(huì)觸發(fā)。當(dāng)選擇完畢點(diǎn)擊確定的時(shí)候 bindchange 事件就會(huì)觸發(fā) 微信小程序的多列下拉框是真的反人

    2024年02月07日
    瀏覽(25)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包