当前位置:网站首页>Object. Usage of keys()

Object. Usage of keys()

2022-07-04 16:45:00 Elephants and ants

Object.keys(obj)

  • Parameters : To return an object that enumerates its own properties
  • Return value : An array of strings representing all enumerable properties of a given object

1. Deal with people , Returns an enumerable array of properties

let person = {
    name:" Zhang San ",age:25,address:" Shenzhen ",getName:function(){
    }};
console.log(Object.keys(person));

 Insert picture description here

Object.keys(item.alarmAttribute || {
    }).length)

2. Handling arrays , Returns an array of index values

let arr = [1,2,3,4,5,6];
console.log(Object.keys(arr));  //["0", "1", "2", "3", "4", "5"]

3. Processing strings , Returns an array of index values

let str = "ikun Hello ";
console.log(Object.keys(str));  // ["0", "1", "2", "3", "4", "5"]

4. Common skills

let person = {
    name:" Zhang San ",age:25,address:" Shenzhen ",getName:function(){
    }};
Object.keys(person).map((key)=>{
    
    console.log(person[key]);  //  Get the corresponding value of the attribute , Do something about it 
})

 Insert picture description here

Project use

Object.keys(dvehicleInfo).forEach(key => {
    
          if (key === "containervolume" && dvehicleInfo[key] === "") {
    
            this.Volume_Static = 1;
            if (dvehicleInfo.container) {
    
              dvehicleInfo.container.split(",").map((item, index) => {
    
                this.containerOptions[index].len = item;
              });
            }
}
原网站

版权声明
本文为[Elephants and ants]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041455112986.html