`
zhangyaochun
  • 浏览: 2614569 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

获取Object的所有keys or values

阅读更多

有的时候,我们需要操作Object中的key和value,我们需要获取对应的所有keys或者values

 

先看看prototype的api设计吧

 

/*
获取keys
*/
keys:function(obj){
   var keys = [];
   for(var pro in obj){
      keys.push(pro);
   }
   return keys;
}

/*
获取values
*/
values:function(obj){
    var values = [];
    for(var pro in obj){
      values.push(obj[pro]);
   }
    return values;
}
 

 

 

 

 

 

 

1、获取Object对应的keys

 

 

/*
*keys-get a array contains all the keys in object*
*@function*
*@param {Object} source*
*@return {Array}*
*@mark we have not check the source is or not object*
*/
ZYC.object.keys = function(source){
    var result=[],
          key,
          _length=0; 
    for(key in source){
	   if(source.hasOwnProperty(key)){
	      result[_length++] = key;
	   }
	}
    return result;
};

 

 

 

2、获取Object对应的values

 

 

/*
*values-get a array contains all the values in object*
*@function*
*@param {Object} source*
*@return {Array}*
*@mark we have not check the source is or not object*
*/
ZYC.object.values = function(source){
    var result=[],key,_length=0;
	for(key in source){
	  if(source.hasOwnProperty(key)){
	     result[_length++] = source[key];
	  }
    }
	return result;
};
 

 

 

分享到:
评论

相关推荐

    object-to-formdata:方便JavaScript函数,可将对象序列化为FormData实例

    const object = { /** * key-value mapping * values can be primitives or objects */} ;const options = { /** * include array indices in FormData keys * defaults to false */ indices : false , /**

    commons-collections4-4.1

    Reference map that allows keys and/or values to be garbage collected under close control Many comparator implementations Many iterator implementations Adapter classes from array and enumerations to ...

    json.js_json2.js

    value any JavaScript value, usually an object or array. replacer an optional parameter that determines how object values are stringified for objects. It can be a function or an array of strings. ...

    最新Ehlib 5.2.84(含完整源代码,支持delphi XE)

    or clising of droped down list of filtered values (STFilter). Location: TSTFilterLocationEh - defines the location STFilter. Under the title (stflUnderTitleFilterEh) or in the title ...

    Databox – Data editor save solution v1.2.1p1

    Static keys generator for accessing tables, entries and values (instead of strings) Demo scenes included PlayMaker support FlowReactor support Powered by FullSerializer and OdinSerializer ...

    Collections

    Since dictionaries are inherently unordered, sorting them requires extracting the keys or values into an array and then sorting that array. This process does not affect the original dictionary. #####...

    java英文笔试

    - **Null Keys and Values**: `Hashtable` does not allow null keys or values, while `HashMap` allows one null key and multiple null values. - **Performance**: `HashMap` generally offers better ...

    2009 达内Unix学习笔记

    集合了 所有的 Unix命令大全 登陆服务器时输入 公帐号 openlab-open123 telnet 192.168.0.23 自己帐号 sd08077-you0 ftp工具 192.168.0.202 tools-toolss 老师测评网址 http://172.16.0.198:8080/poll/ 各个 ...

    EHLib 6.3.171〖D7~XE5〗

    LookupCache: Boolean - Determines whether the values of a lookup field are cached or looked up dynamically every time the current record in the dataset changes. LookupDataSet: TDataSet - Identifies ...

    微软内部资料-SQL性能优化3

    Key range locks are similar to row locks on index keys (whether clustered or not). The locks are placed on individual keys rather than at the node level. The hash value consists of all the key ...

    Chucks_VBA_Reference_rev17.docx

    Explanation of using the `With` statement to group statements that operate on the same object, thus making the code more efficient. **Use “vbNullString” (Page 17)** Instructions on using `...

    SQL Assistant v5.0

    New Code Analysis and Refactoring Functions - smart object, column, and parameter renaming methods; smart code extraction methods; advanced code dependencies analysis Graphical analysis of ...

    微软内部资料-SQL性能优化5

    However, if we are searching for multiple rows, such as duplicate values, or keys in a range, anything more than a small number of rows will make the nonclustered index search very inefficient. ...

    Python Cookbook, 2nd Edition

    Sorting Keys or Indices Basedon the Corresponding Values Recipe 5.5. Sorting Strings with Embedded Numbers Recipe 5.6. Processing All of a List's Items in Random Order Recipe 5.7. Keeping a ...

    11g_plsql_user_guide_and_reference.pdf

    This feature simplifies the process of generating unique values, such as primary keys, during runtime. It eliminates the need for additional procedures or functions to fetch sequence values, ...

    pdf editor

    Enables users to create or edit a shading object, which has a smooth transition between colors across an area. Foxit PDF Editor offers linear and radial gradients for users to choose. 2. Converting ...

    google_app_engine_cheat_sheet_119_1

    - **Additional**: You can pass multiple keys to retrieve multiple model instances at once. **`put(model_instance)`** - **Function**: Saves a model instance to the Datastore. - **Parameters**: - `...

    INTRODUCTION TO PYTHON SCRIPTING FOR MAYA ARTISTS.

    They are used for mapping keys to values and are highly efficient for data retrieval. **Booleans and Comparing Values** Booleans represent logical values: True and False. Comparison operators (==, !...

    A JSON parser in C++

    Unquoted keys: {name: "world"} Default value is Disabled. Assertions JSONxx uses internally JSONXX_ASSERT(...) macro that works both in debug and release mode. Set jsonxx::Settings::Assertions ...

Global site tag (gtag.js) - Google Analytics