728x90
반응형

Front/JS & jQuery 438

[js] .reduce() 그룹화

JavaScript의 .reduce()메서드는 배열의 모든 요소를 ​​순회하며 하나의 가치를 축약(결합)하는 데 사용됩니다.주로 배열의 요소를 합산하거나 특정 형식으로 변환하는 데 활용됩니다.array.reduce(callback[, initialValue]) const pets = [ { type: "dog", name: "Rex" }, { type: "cat", name: "Fluffy" }, { type: "dog", name: "Spot" },];const groupedPets = pets.reduce((acc, pet) => { (acc[pet.type] = acc[pet.type] || []).push(pet.name); return acc;}, {});console.log(group..

Front/JS & jQuery 2025.01.11

[js] Array.from

Array.from()은 JavaScript에서 **유사 배열 객체(array-like object)**나 **반복 가능한 객체(iterable object)**를 배열로 변환하는 데 사용되는 메서드입니다.Array.from(arrayLike, mapFunction, thisArg) arrayLike: 변환하려는 유사 배열 객체나 반복 가능한 객체입니다. 예를 들어, 문자열, Set, Map, NodeList 등이 될 수 있습니다.mapFunction (선택적): 배열로 변환된 후 각 요소에 대해 실행할 함수입니다.thisArg (선택적): mapFunction에서 this로 사용될 값입니다.주요 특징:유사 배열 객체 변환: 배열처럼 인덱스를 가지지만 배열 메서드를 가지지 않는 객체를 배열로 변환합니다...

Front/JS & jQuery 2025.01.10

[js/jQuery] each 와 for 루프 비교 2

https://imswengineer.tistory.com/1179 { // jQuery 요소에 접근 const $option = $(element); console.log($option.val());});장점jQuery 객체를 다룰 때 편리합니다. ($(element)를 사용해서 바로 jQuery 메서드를 호출 가" data-og-host="imswengineer.tistory.com" data-og-source-url="https://imswengineer.tistory.com/1179" data-og-url="https://imswengineer.tistory.com/1179" data-og-image="https://scrap.kakaocdn.net/dn/FaOa2/hyX0qziFvU/wfBwom..

Front/JS & jQuery 2025.01.09

[js] match

match 함수는 JavaScript 문자열(String) 객체의 메서드로,문자열에서 정규식 패턴과 일치하는 값을 찾고, 그 결과를 배열로 반환합니다.주로 정규식을 사용하여 특정 패턴을 추출하거나 확인하는 데 사용됩니다. string.match(regexp)string: 검색할 대상 문자열입니다.regexp: 정규식(RegExp) 객체 또는 문자열입니다.정규식이 아니고 단순 문자열이면 정확히 일치하는 부분을 검색합니다. 반환값정규식에 일치하는 전체 매칭 결과와 캡처 그룹을 포함한 배열을 반환합니다.배열의 첫 번째 요소는 전체 매칭된 문자열입니다.두 번째 요소부터는 캡처 그룹에 해당하는 값들이 순서대로 들어갑니다.일치하는 값이 없으면 null을 반환합니다. 1. 기본 사용 (전체 일치 결과)const st..

Front/JS & jQuery 2025.01.06

[js] include / some

include와 some은 모두 특정 값이나 조건을 확인하는 데 사용되지만, 그 목적과 사용 방식에 차이가 있습니다. 1. includes용도: 배열에 특정 값이 존재하는지 확인할 때 사용합니다.특징:단순히 배열의 요소 중에 해당 값이 포함되어 있는지만 확인합니다.값이 배열에 존재하면 true를 반환, 그렇지 않으면 false를 반환합니다.객체와 같은 복잡한 구조의 데이터에서는 직접 사용하기 어렵습니다.const numbers = [1, 2, 3, 4];console.log(numbers.includes(3)); // trueconsole.log(numbers.includes(5)); // false 객체를 포함한 배열에서는 같은 객체인지 비교할 수 없습니다(참조 비교).const obj = { id: ..

Front/JS & jQuery 2025.01.05

[js/jQuery] each 와 for 루프 비교

1. jQuery each $('option').each((index, element) => { // jQuery 요소에 접근 const $option = $(element); console.log($option.val());});장점jQuery 객체를 다룰 때 편리합니다. ($(element)를 사용해서 바로 jQuery 메서드를 호출 가능)코드가 간결하고 가독성이 좋습니다.index와 element를 쉽게 얻을 수 있습니다.단점내부적으로 jQuery를 사용하므로 약간의 오버헤드가 있습니다.성능이 for 루프보다 느릴 수 있습니다, 특히 많은 요소를 처리할 때. 2. JavaScript for 루프 const options = document.querySelectorAll('option..

Front/JS & jQuery 2025.01.02

[js] 숫자 음수 처리

-이를 포함하는 경우에도 음수를 처리하려면, -의 위치를 ​​확인해야 합니다. 예를 들어, 음수 부호가 있다는 것은 올 수 있도록 규칙을 추가하는 것입니다.let str = "abc-123.45def";let result = str.toString().replace(/[^\d.-]/g, '');// 잘못된 위치의 `-` 처리if ((result.match(/-/g) || []).length > 1 || result.indexOf('-') > 0) { result = result.replace(/-/g, ''); // 음수 부호가 올바르지 않으면 제거}console.log(result); // "-123.45"

Front/JS & jQuery 2025.01.01
728x90
반응형