开发者

How do I create an array containing indexes matching a criteria?

开发者 https://www.devze.com 2022-12-08 08:45 出处:网络
given: var args = new string[] { \"-one开发者_运维问答\",\"two\",\"three\",\"-four\" }; what would magic function need to look like in order to make the following pass?

given:

var args = new string[] { "-one开发者_运维问答",  "two",  "three",  "-four" };

what would magic function need to look like in order to make the following pass?

var result = MagicFunction(args);
Assert.AreEqual(0, result[0]);
Assert.AreEqual(3, result[1]);
Assert.AreEqual(2, result.Length);


int[] MagicFunction(string[] args)
{
    return args.Select((s, i) => new { Value = s, Index = i }) // Associate an index to each item
               .Where(o => o.Value.StartsWith("-"))            // Filter the values
               .Select(o => o.Index)                           // Select the index
               .ToArray();                                     // Convert to array
}


It looks like prototype can do it for you: http://www.prototypejs.org/api/enumerable/find

0

精彩评论

暂无评论...
验证码 换一张
取 消