1.深拷贝
function clone(obj){
var newObj=null;
if(typeof obj=='object'&&obj!==null){
newObj=obj instanceof Array?[]:{};
for(var v in obj){
newObj[v]=clone(obj[v]);
}
}else{
newObj=obj;
}
return newObj;
}
2.将对象转换为一维数组
将一个对象变为二重数组后再存到一重数组内!能方便调用
{foo:‘bar’,baz:43}=> [[“foo”,“bar”],[“baz”,42]]=>[“foo”,“bar”,“baz”,42]
Object.fromEntries() 将二维数组变成对象:↑逆操作
function entries(obj = 0) { //传入需封装的对象
if(obj===0){return 0;}
let arr = [];
obj = Object.entries(obj);
for (const [attr, value] of obj) {
arr[attr] = value;
}
return arr;
}
let o=entries(obj); //use
3.属性是否在对象里
function judgePro(obj,pro){
if(pro in obj){
if(obj.hasOwnProperty(pro)){{
console.log(`${pro}对象在属性里,自有的`);
}else{
console.log(`${pro}对象在属性里,继承的`);
}}else{
console.log(`${pro}对象不在属性里`);
}
}
}
对象.hasOwnProperty(prop) 判断属性在哪个对象,是否是一级或者继承
in 检测可枚举属性
instanceof 检测构造函数是谁
4.查找一个元素里是否包含另一个元素的某一个值
function find(){
let obj={
sun: { value: ["晴天","晴"], path: "./weatherImg/sun.png" },
rain: { value: ["小雨", "阵雨", "大雨", "中雨"], path: "./weatherImg/rain.png" },
cloud: { value: ["多云", "少云", "云"], path: "./weatherImg/cloud.png" }
}
let obj1={value:"晴天"};
var arr=Object.values(obj).find(item=>item.value.includes(obj1.value)).path;
// Object.values(obj).find(item => item.value.includes(weatherObj.weather)).path;
return arr;
}