我的前端之路所涉及的資料結構處理
阿新 • • 發佈:2019-01-02
目錄:1、級聯選擇器;2、將公司結構扁平物件 轉化為 樹形物件;3、氣泡排序;4、Number 陣列中的最大差值;5、生成斐波那契額數列;6、陣列去重
一、級聯選擇器
需要先將下文 part one 中的資料結構轉換成 part two 中的資料結構
// part one var data = [{ "province": "浙江", "city": "杭州", "name": "西湖" }, { "province": "四川", "city": "成都", "name": "錦裡" }, { "province": "四川", "city": "成都", "name": "方所" }, { "province": "四川", "city": "阿壩", "name": "九寨溝" }] // part two var data = [{ "value": "浙江", "children": [{ "value": "杭州", "children": [{ "value": "西湖" }] }] }, { "value": "四川", "children": [{ "value": "成都", "children": [{ "value": "錦裡" }, { "value": "方所" }] }, { "value": "阿壩", "children": [{ "value": "九寨溝" }] }] }]
解決方法:
function transObject(tableData, keys) { let hashTable = {}, res = [] for (let i = 0; i < tableData.length; i++) { let arr = res, cur = hashTable for (let j = 0; j < keys.length; j++) { let key = keys[j], filed = tableData[i][key] if (!cur[filed]) { let pusher = { value: filed }, tmp if (j !== (keys.length - 1)) { tmp = [] pusher.children = tmp } cur[filed] = { $$pos: arr.push(pusher) - 1 } cur = cur[filed] arr = tmp } else { cur = cur[filed] arr = arr[cur.$$pos].children } } } return res } transObject(data, ['province', 'city', 'name'])
此方法使用了 trie 樹原理,好處是:此時 keys 的值與長度無關了,所以這是個通用方法,當級數更多或更少時都可以使用,如:
var data = [{ "province": "浙江", "city": "杭州", "county": "西湖區", "zhen": "三墩鎮", "xiaoqu": "慧仁家園" }, { "province": "安徽", "city": "安慶", "county": "宿松縣", "zhen": "匯口鎮", "xiaoqu": "曹湖村" }, { "province": "安徽", "city": "安慶", "county": "望江縣", "zhen": "華陽鎮", "xiaoqu": "江調村" }] transObject(data, ['province', 'city', 'county', 'zhen', 'xiaoqu'])
二、將公司結構扁平物件 轉化為 樹形物件
var input = {
h3: {
parent: 'h2',
name: '副總經理(市場)'
},
h1: {
parent: 'h0',
name: '公司機構'
},
h7: {
parent: 'h6',
name: '副總經理(總務)'
},
h4: {
parent: 'h3',
name: '銷售經理'
},
h2: {
parent: 'h1',
name: '總經理'
},
h8: {
parent: 'h0',
name: '財務總監'
},
h6: {
parent: 'h4',
name: '倉管總監'
},
h5: {
parent: 'h4',
name: '銷售代表'
},
h0: {
parent: '',
name: 'root'
}
};
function plain2Tree (obj) {
var key, res
for(key in obj) {
var parent = obj[key].parent
if(parent === '') {
res = obj[key]
} else {
obj[parent][key] = obj[key]
}
}
console.log(res)
// return res
}
plain2Tree(input)
三、氣泡排序
var babbleData = [2,1,54,3,13,5,62,12];
function bubbleSort(arr) {
var i = (j = 0);
for (i = 1; i < arr.length; i++) {
for (j = 0; j <= arr.length - i; j++) {
var temp = 0;
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
console.log(arr)
}
bubbleSort(babbleData)
四、Number 陣列中的最大差值
var getMaxProfitData = [2,1,54,3,13,5,62,12];
function getMaxProfit(arr){
var min = arr[0], max = arr[0];
for(var i=0;i<arr.length;i++){
if(arr[i]<min)
min = arr[i];
if(arr[i]>max)
max = arr[i];
}
console.log(max - min)
// return max - min;
}
getMaxProfit(getMaxProfitData)
五、生成斐波那契額數列
function getFibonacci(n) {
var fibarr = [];
var i = 0;
while(i < n) {
if(i <= 1) {
fibarr.push(i);
} else {
fibarr.push(fibarr[i - 1] + fibarr[i - 2])
}
i++;
}
console.log(fibarr)
// return fibarr;
}
getFibonacci(12)
六、陣列去重(方法較多)
// 方法一:
var getUniqueData = [1,2,3,3,5,2,1,2,5,4];
function unique1(array){
var n = [];
for(var i = 0; i < array.length; i++){
if (n.indexOf(array[i]) == -1) {
n.push(array[i]);
}
}
return n;
}
console.log(unique1(getUniqueData));
// 方法二(es6):
var getUniqueData = [1,2,3,3,5,2,1,2,5,4];
var resultarr = [...new Set(getUniqueData)];
console.log(resultarr);
// 方法三:(只是方法一的不同寫法而已)
var getUniqueData = [1,2,3,3,5,2,1,2,5,4];
function unique(arr){
var obj = {}
var data = []
for(var i in arr){
if(!obj[arr[i]]){
obj[arr[i]] = true;
data.push(arr[i]);
}
}
console.log(data)
// return data;
}
unique(getUniqueData)