|
422 | 422 | 2: Promise.resolve('Apple'),
|
423 | 423 | }).then((array) => console.log(array));
|
424 | 424 | }
|
| 425 | + |
| 426 | +// The Customer Array |
| 427 | +let customers = [ |
| 428 | + { |
| 429 | + 'id': 001, |
| 430 | + 'f_name': 'Abby', |
| 431 | + 'l_name': 'Thomas', |
| 432 | + 'gender': 'M', |
| 433 | + 'married': true, |
| 434 | + 'age': 32, |
| 435 | + 'expense': 500, |
| 436 | + 'purchased': ['Shampoo', 'Toys', 'Book'] |
| 437 | + }, |
| 438 | + { |
| 439 | + 'id': 002, |
| 440 | + 'f_name': 'Jerry', |
| 441 | + 'l_name': 'Tom', |
| 442 | + 'gender': 'M', |
| 443 | + 'married': true, |
| 444 | + 'age': 64, |
| 445 | + 'expense': 100, |
| 446 | + 'purchased': ['Stick', 'Blade'] |
| 447 | + }, |
| 448 | + { |
| 449 | + 'id': 003, |
| 450 | + 'f_name': 'Dianna', |
| 451 | + 'l_name': 'Cherry', |
| 452 | + 'gender': 'F', |
| 453 | + 'married': true, |
| 454 | + 'age': 22, |
| 455 | + 'expense': 1500, |
| 456 | + 'purchased': ['Lipstik', 'Nail Polish', 'Bag', 'Book'] |
| 457 | + }, |
| 458 | + { |
| 459 | + 'id': 004, |
| 460 | + 'f_name': 'Dev', |
| 461 | + 'l_name': 'Currian', |
| 462 | + 'gender': 'M', |
| 463 | + 'married': true, |
| 464 | + 'age': 82, |
| 465 | + 'expense': 90, |
| 466 | + 'purchased': ['Book'] |
| 467 | + }, |
| 468 | + { |
| 469 | + 'id': 005, |
| 470 | + 'f_name': 'Maria', |
| 471 | + 'l_name': 'Gomes', |
| 472 | + 'gender': 'F', |
| 473 | + 'married': false, |
| 474 | + 'age': 7, |
| 475 | + 'expense': 300, |
| 476 | + 'purchased': ['Toys'] |
| 477 | + } |
| 478 | +]; |
| 479 | + |
| 480 | +// filter() method |
| 481 | +{ |
| 482 | + // filter example - Build Customer Data for Senior Citizens |
| 483 | + const seniorCustomers = customers.filter((customer) => { |
| 484 | + return (customer.age >= 60) |
| 485 | + }); |
| 486 | + console.log('[filter] Senior Customers = ', seniorCustomers); |
| 487 | +} |
| 488 | + |
| 489 | +// map() method |
| 490 | +{ |
| 491 | + // map example - Build Customer Data with title and full name |
| 492 | + const customersWithFullName = customers.map((customer) => { |
| 493 | + let title = ''; |
| 494 | + if(customer.gender === 'M') { |
| 495 | + title = 'Mr.'; |
| 496 | + } else if(customer.gender === 'F' && customer.married) { |
| 497 | + title = 'Mrs.'; |
| 498 | + } else { |
| 499 | + title = 'Miss'; |
| 500 | + } |
| 501 | + customer['full_name'] = title |
| 502 | + + " " |
| 503 | + + customer.f_name |
| 504 | + + " " |
| 505 | + + customer.l_name; |
| 506 | + return customer; |
| 507 | + }); |
| 508 | + console.log('[map] Customers With Full Name = ' |
| 509 | + , customersWithFullName); |
| 510 | +} |
| 511 | + |
| 512 | +// reduce() method |
| 513 | +{ |
| 514 | + // reduce example - Get the Average Age of |
| 515 | + // Customers who purchased 'Book' |
| 516 | + let count = 0; |
| 517 | + const total = customers.reduce( |
| 518 | + (accumulator, customer, currentIndex, array) => { |
| 519 | + if(customer.purchased.includes('Book')) { |
| 520 | + accumulator = accumulator + customer.age; |
| 521 | + count = count + 1; |
| 522 | + } |
| 523 | + return (accumulator); |
| 524 | + }, |
| 525 | + 0); |
| 526 | + console.log('[reduce] Customer Avg age Purchased Book:' |
| 527 | + , Math.floor(total/count)); |
| 528 | +} |
| 529 | + |
| 530 | +// some() method |
| 531 | +{ |
| 532 | + const hasYoungCustomer = customers.some((customer) => { |
| 533 | + return (customer.age < 10); |
| 534 | + }); |
| 535 | + console.log('[some] Has Young Customer(Age < 10):', hasYoungCustomer); |
| 536 | +} |
| 537 | + |
| 538 | +// find() method |
| 539 | +{ |
| 540 | + const foundYoungCustomer = customers.find((customer) => { |
| 541 | + return (customer.age < 10); |
| 542 | + }); |
| 543 | + console.log('[find] Found Young Customer(Age < 10): ', foundYoungCustomer); |
| 544 | +} |
| 545 | + |
| 546 | +// findIndex() method |
| 547 | +{ |
| 548 | + const index = customers.findIndex((customer) => { |
| 549 | + return (customer.age < 10); |
| 550 | + }); |
| 551 | +} |
| 552 | + |
| 553 | +// findLastIndex() method |
| 554 | +{ |
| 555 | + const index = customers.findLastIndex((customer) => { |
| 556 | + return (customer.age < 10); |
| 557 | + }); |
| 558 | +} |
| 559 | + |
| 560 | +// findLast() method |
| 561 | +{ |
| 562 | + const lastFoundYoungCustomer = customers.findLast((customer) => { |
| 563 | + return (customer.age < 10); |
| 564 | + }); |
| 565 | + console.log('[find] Last Found Young Customer(Age < 10): ', lastFoundYoungCustomer); |
| 566 | +} |
| 567 | + |
| 568 | +// every() method |
| 569 | +{ |
| 570 | + const isThereWindowShopper = customers.every((customer) => { |
| 571 | + return (customer.purchased.length === 0); |
| 572 | + }) |
| 573 | + console.log('[every] Is there a window shopper?', isThereWindowShopper); |
| 574 | +} |
| 575 | + |
| 576 | +// entries() method |
| 577 | +{ |
| 578 | + for (const value of numbers.entries()) { |
| 579 | + console.log(value) |
| 580 | + } |
| 581 | +} |
| 582 | + |
| 583 | +// values() method |
| 584 | +{ |
| 585 | + for (const value of numbers.values()) { |
| 586 | + console.log(value) |
| 587 | + } |
| 588 | +} |
| 589 | + |
| 590 | +// flatMap() method |
| 591 | +{ |
| 592 | + const arr1 = [1, 2, 3, 4]; |
| 593 | + arr1.map(item => item *2); |
| 594 | + arr1.flatMap(item => item *2); |
| 595 | + |
| 596 | + arr1.map(item => [item *2]); |
| 597 | + arr1.flatMap(item => [item *2]); |
| 598 | + |
| 599 | + arr1.map(item => [[item *2]]); |
| 600 | + arr1.flatMap(item => [[item *2]]) |
| 601 | +} |
| 602 | + |
| 603 | +// reduceRight() method |
| 604 | +{ |
| 605 | + let number = [100, 40, 15]; |
| 606 | + |
| 607 | + number.reduceRight((accumulator, current) => { |
| 608 | + return accumulator - current |
| 609 | + }); |
| 610 | +} |
| 611 | + |
| 612 | +// Array method Chaining |
| 613 | +{ |
| 614 | + const marriedCustomers = customers.filter((customer) => { |
| 615 | + return (customer.married); |
| 616 | + }); |
| 617 | + |
| 618 | + const expenseMapped = marriedCustomers.map((marriedCustomer) => { |
| 619 | + return marriedCustomer.expense; |
| 620 | + }); |
| 621 | + |
| 622 | + const totalExpenseMarriedCustomer = expenseMapped.reduce( |
| 623 | + (accum, expense) => { |
| 624 | + return accum + expense; |
| 625 | + }, 0); |
| 626 | + console.log('Total Expense of Married Customers in INR: ' |
| 627 | + , totalExpenseMarriedCustomer); |
| 628 | + |
| 629 | + // After Chining them |
| 630 | + const total = customers |
| 631 | + .filter(customer => customer.married) |
| 632 | + .map(married => married.expense) |
| 633 | + .reduce((accum,expense) => accum + expense); |
| 634 | + console.log('Orchestrated total expense in INR: ', total); |
| 635 | +} |
| 636 | + |
| 637 | + |
| 638 | + |
0 commit comments