js Object内置对象
Object.assign()
静态方法将一个或者多个源对象中所有可枚举的自有属性复制到目标对象,并返回修改后的目标对象。
Object.assign()
1
| Object.assign() 静态方法将一个或者多个源对象中所有可枚举的自有属性复制到目标对象,并返回修改后的目标对象。
|
1 2 3
| const route = { title: 'fei', age: 20 }; const newRoute = Object.assign({}, route, { title: '编辑title' }) console.log(newRoute);
|
Object.getOwnPropertyNames()
1 2 3 4 5 6 7 8
| const object1 = { a: 1, b: 2, c: 3, };
console.log(Object.getOwnPropertyNames(object1));
|
Object.hasOwn()
Object.keys()
Object.values()
Object.entries()
1 2 3 4 5 6 7 8 9 10 11 12
| function convertToUrl(requestParams) { let params = [] Object.entries(requestParams).forEach(([key, value]) => { let param = key + '=' + value params.push(param) }) return params.join('&') }
const obj = { a: 5, b: 7, c: 9 }; convertToUrl(obj)
|
删除对象属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| const params = { name: "fei", age: 18, hobby: "web", other:undefined }
console.log(params);
Object.keys(params).forEach(item=>{ const key = params[item] if (key === '' || key === null || key === undefined) { delete params[item] } })
|
delete 运算符
底部
mdn_Object.assign