Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

find object from list

// main.dart

class Book {
  String id;
  String title;
  double price;

  Book(this.id, this.title, this.price);
}

void main() {
  final List<Book> _books = [
    Book('b1', 'A Blue Novel', 0.99),
    Book('b2', 'A Green Novel', 1.99),
    Book('b3', 'A Cooking Hanbook', 2.99),
    Book('b4', 'Kindacode.com', 0.00)
  ];

  // Find the index of the book whose id is 'b4'
  final int index1 = _books.indexWhere(((book) => book.id == 'b4'));
  if (index1 != -1) {
    print('Index: $index1');
    print('Title: ${_books[index1].title}');
  }

  // Find the index of the fist book whose title contains 'Novel'
  final int index2 = _books.indexWhere((book) => book.title.contains('Novel'));
  if (index2 != -1) {
    print('Index: $index2');
    print('Title: ${_books[index2].title}');
  }
}
Source by www.kindacode.com #
 
PREVIOUS NEXT
Tagged: #find #object #list
ADD COMMENT
Topic
Name
5+9 =