How do I create an array containing indexes matching a criteria?
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
精彩评论