javascript
developer.mozilla
用非数字作为key,就不会把“1” 排在最前面了
let codes = {
"+49": "Germany",
"+41": "Switzerland",
"+44": "Great Britain",
// ..,
"+1": "USA"
};
for (let code in codes) {
alert( +code ); // 49, 41, 44, 1
}
方法1:
const isEmpty = (obj) => {
for (let key in obj) {
return false
}
return true
}
方法2:
const isEmpty = (obj) => JSON.stringify(obj) === '{}'
方法3:
const isEmpty = (obj) => Object.keys(obj).length === 0
即,当对象属性为基本类型时,可以用如下方法进行对象拷贝;一旦含有引用类型属性时,下面的方法将失效。
const obj = {
"type": "1",
"title": "Criss",
"phone": "+86-10-84556681",
}
const obj2={}
Object.assign(obj2, obj)
或缩写
let clone = Object.assign({}, obj);
let clone =JSON.parse(JSON.stringify(obj))
不会拷贝: function、属性值为undefined、
属性值变为null: 属性值为-Infinity、属性值为Infinity、属性值为NaN
或
使用 lodash 库的 _.cloneDeep(obj)。
const obj = {
"title": "Criss",
fun:()=>{
console.log(this.title)
}
}
const obj = {
"title": "Criss",
fun(){
console.log(this.title)
}
}
user.address && user.address.street && user.address.street.name
user.address?.street?.name
?
对其前面的属性进行可选性校验,第一级写了不起作用,所以不用写
obj.method?.()
obj下有方法method才执行
vue 举例
title() {
return this.list?.[0]?.['title'];
},
const { log } = console
const user = {
id:1,
}
let id = Symbol("id");
let id2 = Symbol("id");
user[id]=2
user['id']=3
user[id2]=4
log(user)// { id: 3, [Symbol(id)]: 2, [Symbol(id)]: 4 }
let id = Symbol("id");
let user = {
[id]: 123,
name:'1111'
};
for (let key in user) {
log(key)
}
// name ,忽略了symbol
let id = Symbol("id");
let user = {
[id]: 123,
name:'1111'
};
let clone=Object.keys(user)
log( clone ); // [ 'name' ],忽略了symbol
const { log } = console
let id = Symbol("id");
let user = {
[id]: 123
};
let clone = Object.assign({}, user);
log( clone ); // { [Symbol(id)]: 123 },symbol依然存在
let id = Symbol.for("id");
let id1 = Symbol.for("id");
let user = {
[id]: 123,
[id1]: 234,
name:'1111'
};
log(user)// { name: '1111', [Symbol(id)]: 234 } 只有一个symbol
注意:
1. 找字符串的时候区分大小写
2. 返回值为true / false
const { log } = console
// 在字符串内查找
const str = 'hello world'
log(str.includes('hello'))
// 在数组内查找
const arr1 = ['hello world']
log(arr1.includes('hello'))
// 在数组对象内查找
const arr = [
{
name:'hello'
},
{
name:'world'
},
]
let flag = arr.some(item => item.name.includes('hello') )
log(flag)
slice(indexStart)
slice(indexStart, indexEnd)
substring(indexStart)
substring(indexStart, indexEnd)
let fruits = ["Apple", "Orange", "Plum"];
log( fruits.at(-1) );// 取最后一个元素
fruits.at(0)与fruits[0]一样效果,负数的时候从后向前取
注意:
1. push/pop 方法运行的比较快,而 shift/unshift 比较慢。
后入 / 后出 push / pop
前入 / 前出 unshift / shift
// 实用案例:拿到后台返回数据后,前端需要控制某条数据被选中
let list = [
{
title: '111',
},
{
title: '222',
},
{
title: '333',
},
]
let flag = new Array(list.length).fill(false)
log(flag)
// find 找出满足需求的元素
const list = [
{
title: '111',
},
{
title: '222',
},
{
title: '333',
},
]
const flag = list.find(item => item.title === '222')
log(flag) // { title: '222' }
const list = [
{
title: '111',
},
{
title: '222',
age:18
},
{
title: '333',
},
{
title: '222',
age:20
},
]
const arr = list.map(item =>{
return {
title:item.title
}
} )
log(arr)
/* [
{ title: '111' },
{ title: '222' },
{ title: '333' },
{ title: '222' }
]
*/
const arr = list.map(item =>{
return {
title:'标题 '+item.title
}
})
const arr = list.map(item =>{
return {
...item,
title:'标题 '+item.title
}
})
const arr = list.map(item => item.title)//[ '111', '222', '333', '222' ]
const arr = list.map(item => item.age>18) // [ false, false, false, true ]
// 返回age<20的数据
const isYoung = item => item.age<20
const arr = list.filter(isYoung) // [ { title: '222', age: 20 } ]