Numerical unit conversion

const numberUnitFormat =(value) = > {
            let param = {};
            let k = 10000;
            let sizes = [' '.'万'.'亿'.'$'];
            let i = null;
            if (value < k) {
                param.value = value;
                param.unit = ' ';
            } else {
                i = Math.floor(Math.log(value) / Math.log(k));
                param.value = (value / Math.pow(k, i)).toFixed(2);
                param.unit = sizes[i];
            }
            return param;
        }
Copy the code

NumberUnitFormat (232323343.23) => {"value": "232.32", "unit": "billion"}

Thousandth conversion

const thousandFormat = (num, obj) = > {
            let param = ' ';
            if(! obj) {// Format the thousandth output
                param = num.toLocaleString();
            } else if (obj && obj.lang === 'en') {
                // Format the output to the thousandths with the $sign
                param = num.toLocaleString('en', { style: 'currency'.currency: 'USD' });
            } else if (obj && obj.lang === 'cn') {
                // Format the output with an ¥symbol
                param = num.toLocaleString('cn', { style: 'currency'.currency: 'CNY' });
            } else {
                // Format the thousandth output
                param = num.toLocaleString();
            }
            return param;
        }
Copy the code

Call: thousandFormat(123456789.0, {lang: 'cn'}) => ¥123,456,789.00

Num.replace (/,/gi, “”)

Spun yuan

const convertCentToYuan = (cent) = > {
    const tempArr = cent.toString().padStart(3.'0').split(' ');
    tempArr.splice(-2.0.'. ');
    return tempArr.join(' ');
}
Copy the code

Call: convertCentToYuan(100000) => 1000

Arraylist to tree structure 1

# 1W data, tiled array to tree structure

const arrayToTree = (items, childName = 'children') = > {
    const result = []; // Store the result set
    const itemMap = {}; //
    for (const item of items) {
            const id = item.id;
            const pid = item.parentId;

            if(! itemMap[id]) { itemMap[id] = { [childName]: [], }; } itemMap[id] = { ... item, [childName]: itemMap[id][childName], };const treeItem = itemMap[id];

            if (pid === 0) {
                    result.push(treeItem);
            } else {
                    // if (! itemMap[pid]) {
                    // itemMap[pid] = {
                    // [childName]: [],
                    / /};
                    // }itemMap[pid][childName].push(treeItem); }}return result;
}
Copy the code

Call: arrayToTree(data) => Returns a tree array

Arraylist to tree structure 2

Flat arrays interrotate with JSON tree structures

const listToTree = (list, childName = 'children') = > {
    const res = [];
    const map = list.reduce((res, v) = > ((res[v.id] = v), res), {});

    for (const item of list) {
            if (item.parentId === 0) {
                    res.push(item);
                    continue;
            }
            if (item.parentId in map) {
                    constparent = map[item.parentId]; parent[childName] = parent[childName] || []; parent[childName].push(item); }}return res;
}
Copy the code

Call: listToTree(data) => Returns an array of trees

Tree structure to flat array list

const treeToList = (data, childName = 'children') = > {
    // if (! Array.isArray(data)) {
    // return [];
    // }
    return data.reduce(
        (prev, cur) = >
            prev.concat([cur], treeToList(cur[childName] || [])),
        []
    );
};
Copy the code

Call: treeToList(data) => Returns a flat arraylist

Array to heavy

/** * if the array is pure, use es6 Set to remove the weight, just pass arr *@param  {} Arr array or object array *@param  {} The params array object is deduplicated based on the key value
const uniq = (arr, params) = > {
	if (!Array.isArray(data)) {
		return arr;
	}
	if (params) {
		let obj = {};
		let newArr = arr.reduce((perv, cur) = > {
			obj[cur[params.key]]
				? ' '
				: (obj[cur[params.key]] = true && perv.push(cur));
			returnperv; } []);return newArr;
	} else {
		return Array.from(new Set(arr)); }};Copy the code

Uniq (data, {key: 'id'}) => Returns an arraylist of objects removed by id

Object array decrement

/** * an array of objects that fully match is deduplicated@param  {} Data object array */
const uniqObject = (data) = > {
	let uniques = [];
	let stringify = {};
	for (let i = 0; i < data.length; i++) {
		let keys = Object.keys(data[i]);
		keys.sort(function (a, b) {
			return Number(a) - Number(b);
		});
		let str = ' ';
		for (let j = 0; j < keys.length; j++) {
			str += JSON.stringify(keys[j]);
			str += JSON.stringify(data[i][keys[j]]);
		}
		if(! stringify.hasOwnProperty(str)) { uniques.push(data[i]); stringify[str] =true;
		}
	}
	uniques = uniques;
	return uniques;
};
Copy the code

Call: uniqObject(data) => Returns an arraylist of unduplicated objects