一、方法一

// 自定义方法变量,获取对象的value,keys
var values = function(object) {    
 var values = [];    
 for (var key in object)        
   values.push(object[key]);    
   return values;
 }
var keys = function(object) {    
 var keys = [];   
 for (var key in object)       
    keys.push(key);    
    return keys;
}
// 自定义方法,同上思想
function getObjectKeys(object) {
    var keys = [];
    for (var key in object)
         keys.push(key);
         return keys;
 }
function getObjectValues(object) {
    var values = [];    
    for (var key in object)        
      values.push(object[key]);    
       return values;
 }
 // 对象
 var arrayObject = {    
      key1: "v1",
      key2: "v2",    
      key3: "v3",    
      key4: "v4",    
      key5: "v5"
 };
 console.log(`values = `, values(arrayObject));
 console.log(`keys = `, keys(arrayObject));
 console.log(`values = `, getObjectValues(arrayObject));
 console.log(`keys = `, getObjectKeys(arrayObject));

二、方法二

// 对象
 var arrayObject = {    
      key1: "v1",
      key2: "v2",    
      key3: "v3",    
      key4: "v4",    
      key5: "v5"
 };
var keys = Object.keys(arrayObject).sort();
var values = Object.values(arrayObject);
var keys_p = Object.getOwnPropertyNames(arrayObject);
console.log(`keys =`, keys);
console.log(`values =`, values);
console.log(`keys =`, keys_p);

您已经阅读00:00:00欢迎留言评论,喜欢的话就为作者点个赞或者赏颗糖吧! 分享