In Typescript utilizzo spesso questo motivo.LINQ SingleOrDefault in Typescript
class Vegetable {
constructor(public id: number, public name: string) {
}
}
var vegetable_array = new Array<Vegetable>();
vegetable_array.push(new Vegetable(1, "Carrot"));
vegetable_array.push(new Vegetable(2, "Bean"));
vegetable_array.push(new Vegetable(3, "Peas"));
var id = 1;
var collection = vegetable_array.filter(xvegetable => {
return xvegetable.id == id;
});
var item = collection.length < 1 ? null : collection[0];
console.info(item.name);
Sto pensando di creare un estensione JavaScript simile al metodo LINQ SingleOrDefault dove restituisce null se non è nella matrice.
var item = vegetable.singleOrDefault(xvegetable => {
return xvegetable.id == id});
La mia domanda è se esiste un altro modo per ottenere ciò senza creare un'interfaccia personalizzata. TIA.
Ci sono alcuni slancio in questa direzione. Vedi https://linqjs.codeplex.com/ e http://lord-saumagen.github.io/TS – hlo