`
zhangyaochun
  • 浏览: 2565480 次
  • 性别: 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 ...

    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 ...

    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 ...

    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 ...

    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 ...

    CE中文版-启点CE过NP中文.exe

    If you encounter bugs or have suggestions, please do not hesitate to report them in the forum, bugtracker or by e-mail. And if you have questions, don't hesitate to ask them in the forum Fixes: Fixed...

    FastReport.v4.15 for.Delphi.BCB.Full.Source企业版含ClientServer中文修正版支持D4-XE5

    + added ability to hide Null values in TfrxChartView (TfrxChartView.IgnoreNulls = True) + added ability to set any custom page order for printing (i.e. 3,2,1,5,4 ) + [enterprise] added variables ...

    plsqldev14.0.0.1961x32多语言版+sn.rar

    Function keys have been added for “Search Bar” functions (Focus, Search, Go to next, Go to previous). Substitution variable checkbox and list values can now refer to other variable values as &...

    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/ 各个 ...

    plsqldev14.0.0.1961x64多语言版+sn.rar

    Function keys have been added for “Search Bar” functions (Focus, Search, Go to next, Go to previous). Substitution variable checkbox and list values can now refer to other variable values as &...

    数位板压力测试

    Function Keys 62 FKEYS Programming 62 Information Category 62 B.4. Tilt 62 TILT Programming 63 Information Category 63 B.5. Cursor Mask 63 CSRMASK Programming 64 Information Category 64 B.6. Extended...

    BobBuilder_app

    Theoretically a b+tree is O(N log k N) or log base k of N, now for the typical values of k which are above 200 for example the b+tree should outperform any binary tree because it will use less ...

    NGUI Next-Gen UI 3.0.7 f1.unitypackage

    - NEW: Added the ability to copy/paste all values of the sprites and labels via right-click on the component. - NEW: Added a "next page threshold" value to UICenterOnChild for when you want to swipe ...

Global site tag (gtag.js) - Google Analytics