如何从字典中获取特定类型的键 - 值对(How to Get Key-Value Pairs of a Specific Type Only from Dictionary)
我有一个IDictionary(不是泛型的,所以我在这里处理
object
),并且我只想从它获得该IDictionary中对应于特定键类型K和特定值类型V的元素。十亿种方法来做到这一点,但我发现的最优雅的方式似乎是这样的:Dictionary<K, V> dict = myIDictionary.OfType<KeyValuePair<K, V>>();
这个编译好,所以我假设这个方法可以用于字典(它由IEnumerable声明),但生成的字典是空的,虽然肯定有KVP满足这些条件。 很显然,我假定
KeyValuePair<K, V>
是它期望的格式,因为这是在字典上枚举时使用的,但是它是其他内容吗? 有什么我失踪?I have an IDictionary (not generic, so I am dealing with
object
's here), and I would like to get from it only the elements in that IDictionary which correspond to a specific key type K and a specific value type V. There are a billion ways to do this, but the most elegant way I have found seems to be this:Dictionary<K, V> dict = myIDictionary.OfType<KeyValuePair<K, V>>();
This compiles fine so I am assuming this method CAN work for a dictionary (it's declared by IEnumerable) but the resulting dictionary is empty, although there are definitely KVPs in there that meet those conditions. Obviously I was assuming that
KeyValuePair<K, V>
is the format it is expecting, since that's what's used in enumeration over a dictionary, but is it something else? Is there something I'm missing?
原文:https://stackoverflow.com/questions/18662791
最满意答案
$(function(){ var parentElement = $('#slidercontent'); parentElement.children().each(function(){ //use $(this).children() to get children's children console.log($(this).children()); }); });
$(function(){ var parentElement = $('#slidercontent'); parentElement.children().each(function(){ //use $(this).children() to get children's children console.log($(this).children()); }); });
相关问答
更多-
你应该先得到第一个孩子,然后得到如下name : itemName["-L0uwNE54j88Gu15iuRD"].name 如果第一个子名称未知,您可以使用Object.keys()来获取它: var first_child_name = Object.keys(itemName)[0]; itemName[first_child_name].name var itemName = { "-L0uwNE54j88Gu15iuRD": { id: 1, name: "Cakes ...
-
你是说你的情况是这样的吗? $(function() { var test = { Foo : [ 1, 2, 3], Bar : [4, 5, 6] }; // Your JSON object var prop = "Foo"; //Your select list value $.each(test[prop], function() { alert(this); }); }); 在这种情况下,您想要通过键访问对象.... test[prop] 而不是直接 test.pro ...
-
在jquery对象中使用变量(Use variable in jquery object)[2024-02-13]
您的滞留记录变量超出范围。 尝试在if语句之外声明var staylogged 。 你还需要给它一把钥匙。 var staylogged = ""; // give it a default value if($("input[name='staylogged']").is(":checked")){ var staylogged = "yes"; } else { var staylogged = "false"; } var form_data = { "username": ... -
$(function(){ var parentElement = $('#slidercontent'); parentElement.children().each(function(){ //use $(this).children() to get children's children console.log($(this).children()); }); }); 演示: http : //jsfiddle.net/dirtyd77/VfD5 ...
-
在if条件中比较变量以清空jquery对象[duplicate](Comparing variable to empty jquery object in if condition [duplicate])[2023-08-01]
你想要的是知道集合中是否有0个元素。 像这样做 : if ($(ui.newHeader).length==0) { What you want is to know if there is 0 element in the set. Do it like this : if ($(ui.newHeader).length==0) { -
使用instanceof : console.log(elm instanceof jQuery); 要么: console.log(elm instanceof $); 演示: http : //jsfiddle.net/karim79/8jUKX/ Use instanceof: console.log(elm instanceof jQuery); or: console.log(elm instanceof $); Demo: http://jsfiddle.net/karim79/8jUK ...
-
在android studio中获取SOAP对象中的属性childs(Get property childs in SOAP object in android studio)[2024-02-01]
Codigo , Valor和其他属性是Servicos子项,而不是anyType直接属性。 因此,通过Servicos和cServico的对象链访问这些,如下所示: SoapObject resposta = (SoapObject) envelope.bodyIn; SoapObject body = (SoapObject) resposta.getProperty(0); SoapObject subResposta = (SoapObject) body.getProperty("Servic ... -
JavaScript调用变量作为对象和函数,如jQuery(JavaScript call variable as Object and Function like jQuery)[2021-12-24]
在JavaScript中,函数实际上只是附加代码的对象。 而不是一个普通的对象: var test = {}; test.ajax = function() { /* ajax */ }; ...使用功能: var test = function() { /* test */ }; test.ajax = function() { /* ajax */ }; 在这两种情况下,您都可以访问test.ajax 。 该功能的额外功能是你可以调用test 。 In JavaScript, a function ... -
如何将变量设置为空的但可行的Jquery对象?(How do you set a variable to be an empty, but workable Jquery object?)[2023-05-06]
一个空的jQuery对象声明为: $(); jQuery对象创建的文档: http : //api.jquery.com/jQuery/ 编辑: 听起来你试图通过扩展list来消除if/else语句,无论它是否有任何内容。 是对的吗? 如果是这样,尝试这样的事情: list = $( list.get().concat(item.get()) ); 要么 $.extend(list, item); (假设list以空jQuery对象开头。) 编辑: 由于您在循环中创建元素,因此您始终可以push()转 ... -
counts.theType访问对象的类型counts 。 重要的是要知道对象密钥也可以通过字符串访问。 你只需要使用支架。 例如 : counts['men']; // 299 话虽这么说,您可以将变量传递给括号: counts[theType] 这将返回你想要的。 counts.theType access the key theType of the object counts. It is important to know that object key can also be accesse ...