반응형
JavaScript Array.find 코드 모음
var array = ['cat', 'dog' , 'car'];
var found = array.find(function (element) {
return element == 'dog';
});
document.write(found); // return dog
var array = [10, 20, 30, 40];
var found = array.find(function (element) {
return element > 10;
});
document.write(found); // return 20
const vehicles = [
"car",
"bus",
"truck",
"SUV"
];
const result = vehicles.find(tree => tree.startsWith("c"));
document.write(result) ; // car
const vehicles = [
{ name: "car", count: 4 },
{ name: "bus", count: 5 },
{ name: "SUV", count: 2 }
];
const result = vehicles.find(vehicle => vehicle.name == 'SUV');
console.log(result); // {name: "SUV", count: 2}
const vehicles = [
{ name: "car", count: 4 },
{ name: "bus", count: 5 },
{ name: "SUV", count: 2 }
];
const result = vehicles.find((vehicle, i) => {
if (vehicle.count > 1 && i !== 0) return true;
});
console.log(result); // {name: "bus", count: 5}
const students = [
{name : 'hong' , score : '100'},
{name : 'kim' , score : '60'},
{name : 'seo' , score : '90'},
{name : 'lee' , score : '80'},
{name : 'jung' , score : '40'},
{name : 'han' , score : '50'}
]
//50점 학생 찾기
{
const student = students.find(student => student.score === '40');
console.log(student);
}
//결과: {name: 'jung', score : '40'}
// 실제직업과 다릅니다.
const friends = [
{
name: '양주진',
age: 32,
job: '코인러',
married: false,
},
{
name: '오영제',
age: 32,
job: '랩퍼',
married: false,
},
{
name: '서준형',
age: 32,
job: '2년차 유부남',
married: true,
}
];
// 유부남 찾기
const findedSadGuy = friends.find((friend) => {
return friend.married === true;
})
console.log('슬픈남자 ', findedSadGuy);
//결과=>
{
name: '서준형',
age: 32,
job: '2년차 유부남',
married: true
};
// Input array contain some elements.
var array = [-10, -0.20, 0.30, -40, -50];
// Method (return element > 0).
var found = array.find(function (element) {
return element > 0;
});
// Printing desired values.
console.log(found);
//결과 => 0.3
// Input array contain some elements.
var array = [10, 20, 30, 40, 50];
// Method (return element > 10).
var found = array.find(function (element) {
return element > 20;
});
// Printing desired values.
console.log(found);
//결과=> 30
// Input array contain some elements.
var array = [2, 7, 8, 9];
// Provided testing method (return element > 4).
var found = array.find(function (element) {
return element > 4;
});
// Printing desired values.
console.log(found);
//결과=> 7
refreance: https://dev.to/askavy/array-find-method-in-javascript-kfd
refreance: JavaScript Array find() Method - GeeksforGeeks
728x90
반응형
'Programing > Javascript' 카테고리의 다른 글
javascript String.includes() 함수 샘플코드 모음 (0) | 2023.06.23 |
---|---|
jQuery를 사용하여 페이지 섹션으로 부드럽게 스크롤하는 방법 (0) | 2023.06.16 |
JavaScript 프록시 설정 방법? (0) | 2023.03.13 |
NodeJS vs Python: 비교하기 (0) | 2022.07.08 |
JavaScript Reflection 사용방법 (0) | 2022.07.07 |
댓글