简体   繁体   中英

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
}

看来原型可以为您做到: http//www.prototypejs.org/api/enumerable/find

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM