Nhận các giá trị duy nhất từ hai mảng JavaScript
const arr1 = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p Show
const arr2 = ['b', 'c', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'z function uniqueValuesArr(arr1,arr2) { // lặp qua mảng1 w/ forEach // chỉ đẩy mục duy nhất từ arr1 sang arr3 // gọi hàm w/ mảng ở trên và in đầu ra bảng điều khiển. log(uniqueValuesArr(arr1, arr2)) Khi làm việc với mảng javascript, chúng ta thường gặp phải tình huống cần lấy các giá trị duy nhất từ một mảng. Bài viết này sẽ xem các cách khác nhau để trích xuất các giá trị duy nhất thành một mảng riêng biệt bằng các ví dụ minh họa khác nhau Mục lục Nhận các giá trị duy nhất từ một mảng bằng các bộ lọcBộ lọc của Javascript() method trả về một mảng mới bao gồm tất cả các phần tử vượt qua . Ví dụ. - Lấy các giá trị duy nhất từ mảng [“Xin chào”, “Đây”, “Là”,”Ngôn ngữ”,”Đây”, “Là” ] quảng cáo Mã số. - function getUniqueArray(_array) { // in the newArray get only the elements which pass the test implemented by the filter function. // the test is to check if the element's index is same as the index passed in the argument. let newArray = _array.filter((element, index, array) => array.indexOf(element) === index); return newArray } let myArray = ["Hello", "This", "Is","Language","This", "Is" ]; console.log(getUniqueArray(myArray)); đầu ra. - Đọc thêm
[ 'Hello', 'This', 'Is', 'Language' ] Giải trình. -
Nhận các giá trị duy nhất từ một mảng bằng cách sử dụng SETBộ của Javascript là lưu trữ các giá trị duy nhất . Ví dụ. - Lấy các giá trị duy nhất từ mảng [“Xin chào”, “Đây”, “Là”,”Ngôn ngữ”,”Đây”, “Là” ] Mã số. - function getUniqueArray(_array) { //store the _array elements in the Set and then create a newArray from this Set let newArray =[.. new Set(_array)]; return newArray } let myArray = ["Hello", "This", "Is","Language","This", "Is" ]; console.log(getUniqueArray(myArray)); đầu ra. - [ 'Hello', 'This', 'Is', 'Language' ] Giải trình. -
Nhận các giá trị duy nhất từ một mảng bằng ObjectVí dụ. - Lấy các giá trị duy nhất từ mảng [“Xin chào”, “Đây”, “Là”,”Ngôn ngữ”,”Đây”, “Là” ] Mã số. - function getUniqueArray(_array) { var obj = {}; var uniqueArray = []; for (var i = 0; i < _array.length; i++) { if (obj[_array[i]] == undefined) // add the array elements to object , where the element is key and the same element is value // keys of the object can only have unique values { obj[_array[i]] = i; // add the keys of the object to a new array as elements of the array uniqueArray.push(_array[i]); } } return uniqueArray; } let myArray = ["Hello", "This", "Is","Language","This", "Is" ]; console.log(getUniqueArray(myArray)); đầu ra. - [ 'Hello', 'This', 'Is', 'Language' ] Giải trình. -
Nhận các giá trị duy nhất từ một mảng bằng phép lặpVí dụ. - Lấy các giá trị duy nhất từ mảng [“Xin chào”, “Đây”, “Là”,”Ngôn ngữ”,”Đây”, “Là” ] Mã số. - function containsElement(_array,_value) { for (var i = 0; i < _array.length; i++) { // check if new array already contains the element if (_array[i] === _value) return true; } return false; }; function getUniqueArray(_array) { var newArray = []; for (var i = 0; i < _array.length; i++) { if (!containsElement(newArray,_array[i])) { // if the element is not found in the newArray push it. newArray.push(_array[i]); } } return newArray; } let myArray = ["Hello", "This", "Is","Language","This", "Is" ]; console.log(getUniqueArray(myArray)); đầu ra. - [ 'Hello', 'This', 'Is', 'Language' ] Giải trình. -
|