跳到主要内容

类型转换

对象数组转为树形结构

/**
* 列表转换为树形结构
* 我们需要将列表存储的数据按照层级关系转换成对象,最后生成树形结构,并按顺序记录父节点code
* 请编写一个函数,能够将demoList以及所有与demoList相同数据类型的做下述转换,
* 同demoList => demoListTree, 并给出算法的时间复杂度
*/

// 对象的浅拷贝,改变引用就会改变对象
const demoList = [
{ code: 1, pcode: null, level: 1 },
{ code: 2, pcode: 1, level: 2 },
{ code: 3, pcode: 2, level: 3 },
{ code: 4, pcode: null, level: 1 },
{ code: 5, pcode: 4, level: 2 },
{ code: 6, pcode: null, level: 1 }
]

function listToTree(arr) {
const map = {};
const tree = [];
arr.forEach(element => {
element.pcodes = []
map[element.code] = element;
});
arr.forEach(element => {
const pitem = map[element.pcode];
if(pitem) {
if(!pitem.children) {
pitem.children = [];
}
element.pcodes = [...pitem.pcodes.concat(pitem.code)];
pitem.children.push(element)

}else {
tree.push(element);
}

});

return tree;
};

一个嵌套对象,拍平对象,实现一个key对应一个简单类型的值

function flattenObject(obj) {
const result = {};

for (const key in obj) {
if (obj.hasOwnProperty(key)) {
if (typeof obj[key] === 'object' && obj[key]!== null &&!Array.isArray(obj[key])) {
const flattenedSubObj = flattenObject(obj[key]);
for (const subKey in flattenedSubObj) {
result[key + '.' + subKey] = flattenedSubObj[subKey];
}
} else {
result[key] = obj[key];
}
}
}

return result;
}

const nestedObject = {
a: 1,
b: {
c: 2,
d: {
e: 3
}
}
};

console.log(flattenObject(nestedObject));

写代码实现一个 diff 函数?(深比较)

function deepEqual(a, b) {
// 检查基本类型是否相等
if (a === b) return true;

// 检查是否为null或undefined
if (a == null || b == null) return false;

// 检查是否为对象或数组
if (typeof a !== 'object' || typeof b !== 'object') return false;

// 检查对象或数组的长度是否相等
if (Array.isArray(a) && Array.isArray(b)) {
if (a.length !== b.length) return false;
} else {
// 对象的键的数量是否相等
if (Object.keys(a).length !== Object.keys(b).length) return false;
}

// 递归比较对象或数组的每个属性或元素
for (let key in a) {
if (!b.hasOwnProperty(key) || !deepEqual(a[key], b[key])) {
return false;
}
}

return true;
}

// 示例使用
const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { a: 1, b: { c: 2 } };
const obj3 = { a: 1, b: { c: 3 } };

console.log(deepEqual(obj1, obj2)); // true
console.log(deepEqual(obj1, obj3)); // false

手写嵌套对象由下划线转为驼峰结构

function convertKeysToCamelCase(obj) {
if (Array.isArray(obj)) {
// 如果是数组,递归处理数组中的每个元素
return obj.map(item => convertKeysToCamelCase(item));
}
if (obj && typeof obj === 'object') {
// 如果是对象,递归处理对象的每个属性
const result = {};
Object.keys(obj).forEach(item=> {
const key = item.replace(/(_\w)/g,(match) => match[1].toUpperCase());
result[key] = convertKeysToCamelCase(obj[item])
});
return result;
}
// 如果不是对象或数组,直接返回原值
return obj;
}

// 示例使用
const nestedObject = {
first_name: 'John',
last_name: 'Doe',
address: {
street_address: '123 Main St',
city: 'Anytown',
zip_code: '12345'
}
};

const camelCaseObject = convertKeysToCamelCase(nestedObject);
console.log(camelCaseObject);

ES的class吧,将下面用ES5表示

class person { constructor (name) { this.name = name; } call () { return "I'm" + this.name; } static isPerson() { return true; } }
// 使用构造函数模拟类
function Person(name) {
// 构造函数内部的 this 指向新创建的对象实例
this.name = name;
}

// 为 Person 的原型添加方法
Person.prototype.call = function() {
return "I'm " + this.name;
};

// 为 Person 添加静态方法
Person.isPerson = function() {
return true;
};

// 使用示例
var person1 = new Person('Alice');
console.log(person1.call()); // 输出: I'm Alice

console.log(Person.isPerson()); // 输出: true

实现一个函数 commafy,它接受一个数字作为参数,返回一个字符串,可以把整数部分从右到左每三位数添加一个逗号,如:12000000.11 转化为 12,000,000.11。

function commafy(number) {
// 使用en-US本地化设置来格式化数字,确保使用逗号作为千位分隔符
return number.toLocaleString('en-US');
}
// 示例使用
const num = -12000000.11;
console.log(commafy(num)); // 输出: "12,000,000.11"