|
| 1 | +// find |
| 2 | +// returns object |
| 3 | +// returns first match, if not match undefined |
| 4 | +// great for getting unique value |
| 5 | + |
| 6 | +let people = [ |
| 7 | + { id: 1, name: 'john doe', born: 1983, position: 'developer' }, |
| 8 | + { id: 2, name: 'bobo', born: 1987, position: 'developer' }, |
| 9 | + { id: 3, name: 'peter', born: 1989, position: 'designer' }, |
| 10 | + { id: 1, name: 'sussy', born: 1975, position: 'the boss' }, |
| 11 | + { id: 1, name: 'Jane', born: 1999, position: 'the boss' }, |
| 12 | +]; |
| 13 | + |
| 14 | +const iDs = people.find(function (iDs) { |
| 15 | + // here first match will print but if u use filter will print all obj with id:1 |
| 16 | + return iDs.id === 5 || iDs.position === 'developer'; |
| 17 | +}); |
| 18 | + |
| 19 | +console.log(iDs); |
| 20 | + |
| 21 | +// example again |
| 22 | +const shpMember = ['lutfi', 'Zoro', 'Sanji']; |
| 23 | + |
| 24 | +console.log( |
| 25 | + shpMember.filter(function (items) { |
| 26 | + return items === 'Zoro'; |
| 27 | + }) |
| 28 | +); |
| 29 | +/* |
| 30 | +and output will result lutfi for the first match, cause use find.. but if use map, filter will result as array |
| 31 | +[ 'lutfi', 'Zoro', 'Sanji' ] |
| 32 | +*/ |
0 commit comments