Đầu tiên để có được tất cả các thẻ và lọc các thẻ trùng lặp từ initState. Lưu mảng vào Uniquetags.
Sau đó so sánh các bản Uniquetags với tên khởi đầu, để tạo một sản phẩm mảng khác với các đối tượng và các thẻ và sản phẩm thuộc tính của nó.
const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>arr.indexOf[a,i+1]===-1]
productTags = [];
uniqueTags.forEach[[u]=>{
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productTags.push[{'tag': u, 'product':t.name}];
}]
}]
console.log[JSON.stringify[productTags]];
/*
[
{ "tag": "nature", "product": "Product A" },
{ "tag": "outdoor", "product": "Product A" },
{ "tag": "winter", "product": "Product A" },
{ "tag": "winter", "product": "Product B" },
{ "tag": "hiking", "product": "Product B" },
{ "tag": "camping", "product": "Product A" },
{ "tag": "camping", "product": "Product B" },
{ "tag": "snow", "product": "Product B" },
{ "tag": "vacation", "product": "Product C" },
{ "tag": "family", "product": "Product B" },
{ "tag": "family", "product": "Product C" },
{ "tag": "kids", "product": "Product C" },
{ "tag": "river", "product": "Product C" },
{ "tag": "lake", "product": "Product C" },
{ "tag": "fishing", "product": "Product C" }
] */
[Đã chỉnh sửa sau] Sửa chữa:
Để tạo thành đúng đối tượng, tôi đã thay đổi mã thành:
const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
Phương thức
0 tạo ra một bản sao nông của một phần của một mảng đã cho, được lọc xuống chỉ các phần tử từ mảng đã cho vượt qua thử nghiệm được thực hiện bởi hàm được cung cấp.const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
0 method creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
Thử nó
Cú pháp
// Arrow function
filter[[element] => { /* … */ } ]
filter[[element, index] => { /* … */ } ]
filter[[element, index, array] => { /* … */ } ]
// Callback function
filter[callbackFn]
filter[callbackFn, thisArg]
// Inline callback function
filter[function[element] { /* … */ }]
filter[function[element, index] { /* … */ }]
filter[function[element, index, array]{ /* … */ }]
filter[function[element, index, array] { /* … */ }, thisArg]
Thông số
const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
1Hàm là một vị từ, để kiểm tra từng phần tử của mảng. Trả về một giá trị ép buộc cho
const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
2 để giữ phần tử hoặc cho const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
3 nếu không.Hàm được gọi với các đối số sau:
const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
4Phần tử hiện tại được xử lý trong mảng.
const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
5Chỉ số của phần tử hiện tại được xử lý trong mảng.
const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
6Mảng mà
const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
0 được gọi.const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
8 Tùy chọnOptionalGiá trị để sử dụng là
const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
9 khi thực hiện const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
1.Giá trị trả về
Một bản sao nông của một phần của mảng đã cho, được lọc xuống chỉ các phần tử từ mảng đã cho vượt qua thử nghiệm được thực hiện bởi hàm được cung cấp. Nếu không có phần tử nào vượt qua bài kiểm tra, một mảng trống sẽ được trả về.
Sự mô tả
const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
0 gọi hàm const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
1 được cung cấp một lần cho mỗi phần tử trong một mảng và xây dựng một mảng mới của tất cả các giá trị mà const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
1 trả về một giá trị ép buộc thành const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
2. Các phần tử mảng không vượt qua bài kiểm tra const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
1 được bỏ qua và không được bao gồm trong mảng mới.const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
1 chỉ được gọi cho các chỉ mục mảng đã gán các giá trị. Nó không được gọi cho các khe trống trong các mảng thưa thớt.const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
1 được gọi với ba đối số:- giá trị của phần tử
- chỉ mục của phần tử
- đối tượng mảng đang đi qua
Nếu một tham số
const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
8 được cung cấp cho // Arrow function
filter[[element] => { /* … */ } ]
filter[[element, index] => { /* … */ } ]
filter[[element, index, array] => { /* … */ } ]
// Callback function
filter[callbackFn]
filter[callbackFn, thisArg]
// Inline callback function
filter[function[element] { /* … */ }]
filter[function[element, index] { /* … */ }]
filter[function[element, index, array]{ /* … */ }]
filter[function[element, index, array] { /* … */ }, thisArg]
9, nó sẽ được sử dụng làm giá trị const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
9 của cuộc gọi. Mặt khác, giá trị function isBigEnough[value] {
return value >= 10;
}
const filtered = [12, 5, 8, 130, 44].filter[isBigEnough];
// filtered is [12, 130, 44]
1 sẽ được sử dụng làm giá trị const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
9 của nó. Giá trị const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
9 cuối cùng có thể quan sát được bởi const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
1 được xác định theo các quy tắc thông thường để xác định const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
9 được nhìn thấy bởi một hàm.Phương pháp
const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
0 là một phương thức sao chép. Nó không thay đổi const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
9 mà thay vào đó trả về một bản sao nông chứa các phần tử giống như các phần tử từ mảng gốc [với một số được lọc ra].Phạm vi của các phần tử được xử lý bởi
const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
0 được đặt trước khi gọi đầu tiên của const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
1. Các yếu tố được gán cho các chỉ mục đã được truy cập hoặc các chỉ mục bên ngoài phạm vi, sẽ không được truy cập bởi const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
1. Nếu các phần tử hiện tại của mảng bị xóa giống như cách chúng sẽ không được truy cập.CẢNH BÁO: Sửa đổi đồng thời loại được mô tả trong đoạn trước thường xuyên dẫn đến mã khó hiểu và thường phải tránh [ngoại trừ trong các trường hợp đặc biệt]. Concurrent modification of the kind described in the previous paragraph frequently leads to hard-to-understand code and is generally to be avoided [except in special cases].
Phương pháp
const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
0 là chung chung. Nó chỉ mong đợi giá trị const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
9 có thuộc tính const array = [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
function isPrime[num] {
for [let i = 2; num > i; i++] {
if [num % i === 0] {
return false;
}
}
return num > 1;
}
console.log[array.filter[isPrime]]; // [2, 3, 5, 7, 11, 13]
3 và các thuộc tính được khóa.
Ví dụ
Lọc ra tất cả các giá trị nhỏ
Ví dụ sau sử dụng
const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
0 để tạo một mảng được lọc có tất cả các phần tử có giá trị nhỏ hơn const array = [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
function isPrime[num] {
for [let i = 2; num > i; i++] {
if [num % i === 0] {
return false;
}
}
return num > 1;
}
console.log[array.filter[isPrime]]; // [2, 3, 5, 7, 11, 13]
5 bị xóa.function isBigEnough[value] {
return value >= 10;
}
const filtered = [12, 5, 8, 130, 44].filter[isBigEnough];
// filtered is [12, 130, 44]
Tìm tất cả các số nguyên tố trong một mảng
Ví dụ sau trả về tất cả các số nguyên tố trong mảng:
const array = [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
function isPrime[num] {
for [let i = 2; num > i; i++] {
if [num % i === 0] {
return false;
}
}
return num > 1;
}
console.log[array.filter[isPrime]]; // [2, 3, 5, 7, 11, 13]
Lọc các mục không hợp lệ từ JSON
Ví dụ sau đây sử dụng
const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
0 để tạo JSON được lọc của tất cả các phần tử với số không, số const array = [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
function isPrime[num] {
for [let i = 2; num > i; i++] {
if [num % i === 0] {
return false;
}
}
return num > 1;
}
console.log[array.filter[isPrime]]; // [2, 3, 5, 7, 11, 13]
7.const arr = [
{ id: 15 },
{ id: -1 },
{ id: 0 },
{ id: 3 },
{ id: 12.2 },
{},
{ id: null },
{ id: NaN },
{ id: 'undefined' },
];
let invalidEntries = 0;
function filterByID[item] {
if [Number.isFinite[item.id] && item.id !== 0] {
return true;
}
invalidEntries++;
return false;
}
const arrByID = arr.filter[filterByID];
console.log['Filtered Array\n', arrByID];
// Filtered Array
// [{ id: 15 }, { id: -1 }, { id: 3 }, { id: 12.2 }]
console.log['Number of Invalid Entries = ', invalidEntries];
// Number of Invalid Entries = 5
Tìm kiếm trong mảng
Ví dụ sau sử dụng
const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
0 để lọc nội dung mảng dựa trên tiêu chí tìm kiếm.const fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];
/**
* Filter array items based on search criteria [query]
*/
function filterItems[arr, query] {
return arr.filter[[el] => el.toLowerCase[].includes[query.toLowerCase[]]];
}
console.log[filterItems[fruits, 'ap']]; // ['apple', 'grapes']
console.log[filterItems[fruits, 'an']]; // ['banana', 'mango', 'orange']
Sử dụng Filter [] trên các mảng thưa thớt
const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
0 sẽ bỏ qua các khe trống.console.log[[1, , undefined].filter[[x] => x === undefined]]; // [undefined]
console.log[[1, , undefined].filter[[x] => x !== 2]]; // [1, undefined]
Gọi bộ lọc [] trên các đối tượng không phải là
Phương thức
const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
0 đọc thuộc tính const array = [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
function isPrime[num] {
for [let i = 2; num > i; i++] {
if [num % i === 0] {
return false;
}
}
return num > 1;
}
console.log[array.filter[isPrime]]; // [2, 3, 5, 7, 11, 13]
3 của const initialState = [
{id:1 ,name: 'Product A', image: 'pic-001.jpg', tags: ['nature', 'camping', 'winter', 'outdoor']},
{id:2 ,name: 'Product B', image: 'pic-002.jpg', tags: ['winter', 'hiking', 'family', 'camping', 'snow']},
{id:3 ,name: 'Product C', image: 'pic-003.jpg', tags: ['vacation', 'family', 'kids', 'river', 'lake', 'fishing']}
]
let allTags = [];
initialState.map[[t]=>t.tags].forEach[[a]=>a.forEach[[b]=>allTags.push[b]]]
let uniqueTags = allTags.filter[[a,i,arr]=>!arr.includes[a,i+1]]
productTags = [];
uniqueTags.forEach[[u]=>{
let productName = [];
initialState.forEach[[t]=>{
if[t.tags.includes[u]]
productName.push[t.name];
}]
productTags.push[{tag:u, products:productName}];
}]
console.log[JSON.stringify[productTags]];
/*
productTags = [
{"tag":"nature","products":["Product A"]},
{"tag":"outdoor","products":["Product A"]},
{"tag":"winter","products":["Product A","Product B"]},
{"tag":"hiking","products":["Product B"]},
{"tag":"camping","products":["Product A","Product B"]},
{"tag":"snow","products":["Product B"]},
{"tag":"vacation","products":["Product C"]},
{"tag":"family","products":["Product B","Product C"]},
{"tag":"kids","products":["Product C"]},
{"tag":"river","products":["Product C"]},
{"tag":"lake","products":["Product C"]},
{"tag":"fishing","products":["Product C"]}
]
*/
9 và sau đó truy cập vào từng chỉ mục số nguyên.const arrayLike = {
length: 3,
0: "a",
1: "b",
2: "c",
};
console.log[
Array.prototype.filter.call[arrayLike, [x] => x { /* … */ } ]
filter[[element, index] => { /* … */ } ]
filter[[element, index, array] => { /* … */ } ]
// Callback function
filter[callbackFn]
filter[callbackFn, thisArg]
// Inline callback function
filter[function[element] { /* … */ }]
filter[function[element, index] { /* … */ }]
filter[function[element, index, array]{ /* … */ }]
filter[function[element, index, array] { /* … */ }, thisArg]
9 khi mảng được sửa đổi.// Modifying each word
let words = ['spray', 'limit', 'exuberant', 'destruction', 'elite', 'present'];
const modifiedWords = words.filter[[word, index, arr] => {
arr[index + 1] += ' extra';
return word.length