속성 과정 - 13 유용한 자바스크립트 배열 방법
                                            
                                                
                                                
                                                
                                                
                                                
                                                 19037 단어  programmingwebdevjavascriptnode
                    
JavaScript 배열 방법은 일반적으로 필요한 데이터 변환을 수행하는 데 필요한 믿을 수 없고 우아한 방법을 제공합니다.Stack Overflow의 공헌자로서 나는 대상 그룹을 어떻게 이런저런 방식으로 조종하는지에 관한 문제를 자주 본다.이것은 왕왕 수조 방법의 완벽한 용례이다.
나는 이곳에서 유사한 방법으로 조직되어 때로는 합병될 수 있는 서로 다른 수조 방법을 소개할 것이다.이 목록은 전면적이지 않습니다. MDN (내가 가장 좋아하는 자바스크립트 참조) 에서 토론한 모든 내용을 되돌아보고 실천하시기를 권장합니다.
매핑, 필터링, 감소
javascript수 그룹 방법
map,filter,reduce에 약간의 혼동이 존재한다.이것들은 그룹을 변환하거나 집합 값을 되돌려 주는 유용한 방법이다.const arr = [1, 2, 3, 4, 5, 6];
const mapped = arr.map(el => el + 20);
console.log(mapped);
// [21, 22, 23, 24, 25, 26]
const arr = [1, 2, 3, 4, 5, 6];
const filtered = arr.filter(el => el === 2 || el === 4);
console.log(filtered);
// [2, 4]
const arr = [1, 2, 3, 4, 5, 6];
const reduced = arr.reduce((total, current) => total + current, 0);
console.log(reduced);
// 21
const arr = [];
const reduced = arr.reduce((total, current) => total + current);
console.log(reduced);
// Uncaught TypeError: Reduce of empty array with no initial value
색인
수조 방법
find,findIndex과indexOf는 통상적으로 합병할 수 있다.다음과 같이 사용합니다.const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const found = arr.find(el => el > 5);
console.log(found);
// 6
for 순환을 깨뜨릴 것이다. 일치하는 것을 찾으면!const arr = ['Nick', 'Frank', 'Joe', 'Frank'];
const foundIndex = arr.findIndex(el => el === 'Frank');
console.log(foundIndex);
// 1
const arr = ['Nick', 'Frank', 'Joe', 'Frank'];
const foundIndex = arr.indexOf('Frank');
console.log(foundIndex);
// 1
밀어내기, 팝업, 이동, 이동 취소
여러 가지 좋은 수조 방법은 수조에서 맞춤형 방식으로 요소를 추가하거나 삭제하는 데 도움을 줄 수 있다.
const arr = [1, 2, 3, 4];
const pushed = arr.push(5);
console.log(arr);
// [1, 2, 3, 4, 5]
console.log(pushed);
// 5
const arr = [1, 2, 3, 4];
const popped = arr.pop();
console.log(arr);
// [1, 2, 3]
console.log(popped);
// 4
const arr = [1, 2, 3, 4];
const shifted = arr.shift();
console.log(arr);
// [2, 3, 4]
console.log(shifted);
// 1
const arr = [1, 2, 3, 4];
const unshifted = arr.unshift(5, 6, 7);
console.log(arr);
// [5, 6, 7, 1, 2, 3, 4]
console.log(unshifted);
// 7
조인트
이 방법들은 그룹 서브집합을 수정하거나, 그룹 서브집합을 되돌려줍니다.
The following code sample can be read as: at position 1 of the array, remove 0 elements and insert b.
const arr = ['a', 'c', 'd', 'e'];
arr.splice(1, 0, 'b');
console.log(arr);
// ['a', 'b', 'c', 'd', 'e']
const arr = ['a', 'b', 'c', 'd', 'e'];
const sliced = arr.slice(2, 4);
console.log(sliced);
// ['c', 'd']
console.log(arr);
// ['a', 'b', 'c', 'd', 'e']
분류하다
const arr = [1, 7, 3, -1, 5, 7, 2];
const sorter = (firstEl, secondEl) => firstEl - secondEl;
arr.sort(sorter);
console.log(arr);
// [-1, 1, 2, 3, 5, 7, 7]
Reference
이 문제에 관하여(속성 과정 - 13 유용한 자바스크립트 배열 방법), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/nas5w/crash-course-13-helpful-javascript-array-methods-2ikm텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)