Kiểm tra xem mảng không bao gồm giá trị #
Để kiểm tra xem một mảng không bao gồm một giá trị, hãy sử dụng toán tử logic không [!] Để phủ định cuộc gọi đến phương thức
0. Toán tử không [!] Trả về var arrayObj = [{name:"bull", text: "sour"},
{ name: "tom", text: "tasty" },
{ name: "tom", text: "tasty" }
]
var index = arrayObj.findIndex[x => x.name=="bob"];
// here you can check specific property for an object whether it exist in your array or not
index === -1 ? arrayObj.push[{your_object}] : console.log["object already exists"]
1 khi được gọi trên giá trị var arrayObj = [{name:"bull", text: "sour"},
{ name: "tom", text: "tasty" },
{ name: "tom", text: "tasty" }
]
var index = arrayObj.findIndex[x => x.name=="bob"];
// here you can check specific property for an object whether it exist in your array or not
index === -1 ? arrayObj.push[{your_object}] : console.log["object already exists"]
2 và ngược lại.var arrayObj = [{name:"bull", text: "sour"},
{ name: "tom", text: "tasty" },
{ name: "tom", text: "tasty" }
]
var index = arrayObj.findIndex[x => x.name=="bob"];
// here you can check specific property for an object whether it exist in your array or not
index === -1 ? arrayObj.push[{your_object}] : console.log["object already exists"]
Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false if [notIncludesC] { console.log['✅ the value c is NOT included in the array']; } else { console.log['⛔️ the value c is included in the array']; }
Chúng tôi đã sử dụng toán tử logic không [!] Để phủ định các cuộc gọi đến phương thức mảng.
Cách tiếp cận này cho phép chúng tôi kiểm tra xem một giá trị cụ thể không có trong mảng.
Ví dụ đầu tiên của chúng tôi kiểm tra xem giá trị
var arrayObj = [{name:"bull", text: "sour"},
{ name: "tom", text: "tasty" },
{ name: "tom", text: "tasty" }
]
var index = arrayObj.findIndex[x => x.name=="bob"];
// here you can check specific property for an object whether it exist in your array or not
index === -1 ? arrayObj.push[{your_object}] : console.log["object already exists"]
3 không được chứa trong mảng và trả về var arrayObj = [{name:"bull", text: "sour"},
{ name: "tom", text: "tasty" },
{ name: "tom", text: "tasty" }
]
var index = arrayObj.findIndex[x => x.name=="bob"];
// here you can check specific property for an object whether it exist in your array or not
index === -1 ? arrayObj.push[{your_object}] : console.log["object already exists"]
2.Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Chuỗi
var arrayObj = [{name:"bull", text: "sour"},
{ name: "tom", text: "tasty" },
{ name: "tom", text: "tasty" }
]
var index = arrayObj.findIndex[x => x.name=="bob"];
// here you can check specific property for an object whether it exist in your array or not
index === -1 ? arrayObj.push[{your_object}] : console.log["object already exists"]
5 được chứa trong mảng, do đó biểu thức trả về var arrayObj = [{name:"bull", text: "sour"},
{ name: "tom", text: "tasty" },
{ name: "tom", text: "tasty" }
]
var index = arrayObj.findIndex[x => x.name=="bob"];
// here you can check specific property for an object whether it exist in your array or not
index === -1 ? arrayObj.push[{your_object}] : console.log["object already exists"]
1.Dưới đây là một số ví dụ khác về việc sử dụng toán tử logic không [!].
Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
Bạn có thể tưởng tượng rằng toán tử logic không [!] Trước tiên chuyển đổi giá trị thành
var arrayObj = [{name:"bull", text: "sour"},
{ name: "tom", text: "tasty" },
{ name: "tom", text: "tasty" }
]
var index = arrayObj.findIndex[x => x.name=="bob"];
// here you can check specific property for an object whether it exist in your array or not
index === -1 ? arrayObj.push[{your_object}] : console.log["object already exists"]
7 và sau đó lật giá trị.Khi bạn phủ định giá trị giả, toán tử trả về
2. Trong tất cả các trường hợp khác, nó trả về var arrayObj = [{name:"bull", text: "sour"},
{ name: "tom", text: "tasty" },
{ name: "tom", text: "tasty" }
]
var index = arrayObj.findIndex[x => x.name=="bob"];
// here you can check specific property for an object whether it exist in your array or not
index === -1 ? arrayObj.push[{your_object}] : console.log["object already exists"]
1.var arrayObj = [{name:"bull", text: "sour"},
{ name: "tom", text: "tasty" },
{ name: "tom", text: "tasty" }
]
var index = arrayObj.findIndex[x => x.name=="bob"];
// here you can check specific property for an object whether it exist in your array or not
index === -1 ? arrayObj.push[{your_object}] : console.log["object already exists"]
Các giá trị giả trong JavaScript là:
// check if an element exists in array using a comparer function
// comparer : function[currentElement]
Array.prototype.inArray = function[comparer] {
for[var i=0; i < this.length; i++] {
if[comparer[this[i]]] return true;
}
return false;
};
// adds an element to the array if it does not already exist using a comparer
// function
Array.prototype.pushIfNotExist = function[element, comparer] {
if [!this.inArray[comparer]] {
this.push[element];
}
};
var array = [{ name: "tom", text: "tasty" }];
var element = { name: "tom", text: "tasty" };
array.pushIfNotExist[element, function[e] {
return e.name === element.name && e.text === element.text;
}];
0, // check if an element exists in array using a comparer function
// comparer : function[currentElement]
Array.prototype.inArray = function[comparer] {
for[var i=0; i < this.length; i++] {
if[comparer[this[i]]] return true;
}
return false;
};
// adds an element to the array if it does not already exist using a comparer
// function
Array.prototype.pushIfNotExist = function[element, comparer] {
if [!this.inArray[comparer]] {
this.push[element];
}
};
var array = [{ name: "tom", text: "tasty" }];
var element = { name: "tom", text: "tasty" };
array.pushIfNotExist[element, function[e] {
return e.name === element.name && e.text === element.text;
}];
1, chuỗi trống, // check if an element exists in array using a comparer function
// comparer : function[currentElement]
Array.prototype.inArray = function[comparer] {
for[var i=0; i < this.length; i++] {
if[comparer[this[i]]] return true;
}
return false;
};
// adds an element to the array if it does not already exist using a comparer
// function
Array.prototype.pushIfNotExist = function[element, comparer] {
if [!this.inArray[comparer]] {
this.push[element];
}
};
var array = [{ name: "tom", text: "tasty" }];
var element = { name: "tom", text: "tasty" };
array.pushIfNotExist[element, function[e] {
return e.name === element.name && e.text === element.text;
}];
2, // check if an element exists in array using a comparer function
// comparer : function[currentElement]
Array.prototype.inArray = function[comparer] {
for[var i=0; i < this.length; i++] {
if[comparer[this[i]]] return true;
}
return false;
};
// adds an element to the array if it does not already exist using a comparer
// function
Array.prototype.pushIfNotExist = function[element, comparer] {
if [!this.inArray[comparer]] {
this.push[element];
}
};
var array = [{ name: "tom", text: "tasty" }];
var element = { name: "tom", text: "tasty" };
array.pushIfNotExist[element, function[e] {
return e.name === element.name && e.text === element.text;
}];
3 và var arrayObj = [{name:"bull", text: "sour"},
{ name: "tom", text: "tasty" },
{ name: "tom", text: "tasty" }
]
var index = arrayObj.findIndex[x => x.name=="bob"];
// here you can check specific property for an object whether it exist in your array or not
index === -1 ? arrayObj.push[{your_object}] : console.log["object already exists"]
1.Đọc thêm #
- Phần tử mảng.push [] nếu không tồn tại bằng cách sử dụng javascript
- Kiểm tra xem mảng có chứa các phần tử trống trong javascript không
- Kiểm tra xem mảng có chứa một đối tượng trong javascript không
- Kiểm tra xem một mảng có chứa các bản sao trong JavaScript không
- Kiểm tra xem mảng chỉ chứa các số trong javascript
Đối với một mảng các chuỗi [nhưng không phải là một mảng các đối tượng], bạn có thể kiểm tra xem một mục có tồn tại bằng cách gọi
// check if an element exists in array using a comparer function
// comparer : function[currentElement]
Array.prototype.inArray = function[comparer] {
for[var i=0; i < this.length; i++] {
if[comparer[this[i]]] return true;
}
return false;
};
// adds an element to the array if it does not already exist using a comparer
// function
Array.prototype.pushIfNotExist = function[element, comparer] {
if [!this.inArray[comparer]] {
this.push[element];
}
};
var array = [{ name: "tom", text: "tasty" }];
var element = { name: "tom", text: "tasty" };
array.pushIfNotExist[element, function[e] {
return e.name === element.name && e.text === element.text;
}];
5 không và nếu nó không thì chỉ cần đẩy mục vào mảng:push the item into the array:var newItem = "NEW_ITEM_TO_ARRAY";
var array = ["OLD_ITEM_1", "OLD_ITEM_2"];
array.indexOf[newItem] === -1 ? array.push[newItem] : console.log["This item already exists"];
console.log[array]
Nisal Edu
6.7714 Huy hiệu vàng25 Huy hiệu bạc33 Huy hiệu Đồng4 gold badges25 silver badges33 bronze badges
Đã trả lời ngày 17 tháng 4 năm 2016 lúc 23:32Apr 17, 2016 at 23:32
Jiří Zahálkajiří ZahálkaJiří Zahálka
7.7802 Huy hiệu vàng20 Huy hiệu bạc17 Huy hiệu đồng2 gold badges20 silver badges17 bronze badges
10
Nó khá dễ dàng để sử dụng hàm
// check if an element exists in array using a comparer function
// comparer : function[currentElement]
Array.prototype.inArray = function[comparer] {
for[var i=0; i < this.length; i++] {
if[comparer[this[i]]] return true;
}
return false;
};
// adds an element to the array if it does not already exist using a comparer
// function
Array.prototype.pushIfNotExist = function[element, comparer] {
if [!this.inArray[comparer]] {
this.push[element];
}
};
var array = [{ name: "tom", text: "tasty" }];
var element = { name: "tom", text: "tasty" };
array.pushIfNotExist[element, function[e] {
return e.name === element.name && e.text === element.text;
}];
6, có chức năng như một đối số:var arrayObj = [{name:"bull", text: "sour"},
{ name: "tom", text: "tasty" },
{ name: "tom", text: "tasty" }
]
var index = arrayObj.findIndex[x => x.name=="bob"];
// here you can check specific property for an object whether it exist in your array or not
index === -1 ? arrayObj.push[{your_object}] : console.log["object already exists"]
Đã trả lời ngày 9 tháng 6 năm 2016 lúc 13:16Jun 9, 2016 at 13:16
Ashish Yadavashish YadavAshish Yadav
2.8811 Huy hiệu vàng14 Huy hiệu bạc23 Huy hiệu đồng1 gold badge14 silver badges23 bronze badges
1
Bạn có thể mở rộng nguyên mẫu mảng bằng một phương thức tùy chỉnh:
// check if an element exists in array using a comparer function
// comparer : function[currentElement]
Array.prototype.inArray = function[comparer] {
for[var i=0; i < this.length; i++] {
if[comparer[this[i]]] return true;
}
return false;
};
// adds an element to the array if it does not already exist using a comparer
// function
Array.prototype.pushIfNotExist = function[element, comparer] {
if [!this.inArray[comparer]] {
this.push[element];
}
};
var array = [{ name: "tom", text: "tasty" }];
var element = { name: "tom", text: "tasty" };
array.pushIfNotExist[element, function[e] {
return e.name === element.name && e.text === element.text;
}];
Scunliffe
61.4K25 Huy hiệu vàng126 Huy hiệu bạc160 Huy hiệu Đồng25 gold badges126 silver badges160 bronze badges
Đã trả lời ngày 1 tháng 1 năm 2010 lúc 11:12Jan 1, 2010 at 11:12
Darin Dimitrovdarin DimitrovDarin Dimitrov
1.0M267 Huy hiệu vàng3257 Huy hiệu bạc2914 Huy hiệu đồng267 gold badges3257 silver badges2914 bronze badges
7
//api.jquery.com/jQuery.unique/
var cleanArray = $.unique[clutteredArray];
bạn cũng có thể quan tâm đến makearray
Ví dụ trước là tốt nhất để nói rằng kiểm tra xem nó có tồn tại trước khi đẩy không. Tôi thấy trong nhận thức muộn, nó cũng nói rằng bạn có thể tuyên bố nó là một phần của nguyên mẫu [tôi đoán đó là phần mở rộng lớp AKA], vì vậy không có sự tăng cường lớn nào dưới đây.
Ngoại trừ tôi không chắc liệu Indexof có phải là tuyến đường nhanh hơn không? có thể.
Array.prototype.pushUnique = function [item]{
if[this.indexOf[item] == -1] {
//if[jQuery.inArray[item, this] == -1] {
this.push[item];
return true;
}
return false;
}
Đã trả lời ngày 13 tháng 10 năm 2012 lúc 23:02Oct 13, 2012 at 23:02
2
Như thế này?
var item = "Hello World";
var array = [];
if [array.indexOf[item] === -1] array.push[item];
Với đối tượng
var item = {name: "tom", text: "tasty"}
var array = [{}]
if [!array.find[o => o.name === 'tom' && o.text === 'tasty']]
array.push[item]
clami219
2.9081 Huy hiệu vàng29 Huy hiệu bạc43 Huy hiệu đồng1 gold badge29 silver badges43 bronze badges
Đã trả lời ngày 4 tháng 3 năm 2017 lúc 17:35Mar 4, 2017 at 17:35
Ronnie Roystonronnie RoystonRonnie Royston
Huy hiệu vàng 14,7K66 gold badges73 silver badges84 bronze badges
3
Tôi biết đây là một câu hỏi rất cũ, nhưng nếu bạn đang sử dụng ES6, bạn có thể sử dụng một phiên bản rất nhỏ:
0Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Rất dễ dàng, lúc đầu, hãy thêm một bộ lọc loại bỏ mục - nếu nó đã tồn tại, sau đó thêm nó thông qua một concat.
Đây là một ví dụ thực tế hơn:
1Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Nếu mảng bạn chứa các đối tượng, bạn có thể điều chỉnh chức năng bộ lọc như thế này:
2Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Đã trả lời ngày 24 tháng 4 năm 2017 lúc 14:28Apr 24, 2017 at 14:28
2
Đẩy mạnh
3Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Trong phương pháp đơn giản
4Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Nếu mảng chỉ chứa các loại nguyên thủy/ mảng đơn giản
5Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Đã trả lời ngày 7 tháng 2 năm 2019 lúc 13:53Feb 7, 2019 at 13:53
GopalgopalGopal
1.96921 Huy hiệu bạc16 Huy hiệu đồng21 silver badges16 bronze badges
2
Sử dụng thư viện JS như Undercore.js vì những lý do này chính xác. Sử dụng: Liên minh: Tính toán sự kết hợp của các mảng thông qua: Danh sách các mục độc đáo, theo thứ tự, có mặt trong một hoặc nhiều mảng.
6Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Đã trả lời ngày 2 tháng 6 năm 2014 lúc 18:39Jun 2, 2014 at 18:39
Ronen Rabinovicironen RabinoviciRonen Rabinovici
8.1843 Huy hiệu vàng30 Huy hiệu bạc44 Huy hiệu đồng3 gold badges30 silver badges44 bronze badges
2
Mã dễ dàng, nếu 'indexof' return '-1', điều đó có nghĩa là phần tử đó không nằm trong mảng thì điều kiện '=== -1' truy xuất đúng/sai.
'&&' Toán tử có nghĩa là 'và', vì vậy nếu điều kiện đầu tiên là đúng, chúng tôi sẽ đẩy nó vào mảng.
7Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Đã trả lời ngày 8 tháng 1 năm 2020 lúc 8:07Jan 8, 2020 at 8:07
Eric Valeroeric ValeroEric Valero
4324 Huy hiệu bạc7 Huy hiệu đồng4 silver badges7 bronze badges
4
Tôi sẽ đề nghị bạn sử dụng một bộ,
Bộ chỉ cho phép các mục duy nhất, tự động giải quyết vấn đề của bạn.
Bộ có thể được khai báo như vậy:
8Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Đã trả lời ngày 23 tháng 4 năm 2019 lúc 22:49Apr 23, 2019 at 22:49
Giuffesyogiraffesyogiraffesyo
4.5311 Huy hiệu vàng28 Huy hiệu bạc38 Huy hiệu đồng1 gold badge28 silver badges38 bronze badges
3
Không chắc chắn về tốc độ, nhưng
// check if an element exists in array using a comparer function
// comparer : function[currentElement]
Array.prototype.inArray = function[comparer] {
for[var i=0; i < this.length; i++] {
if[comparer[this[i]]] return true;
}
return false;
};
// adds an element to the array if it does not already exist using a comparer
// function
Array.prototype.pushIfNotExist = function[element, comparer] {
if [!this.inArray[comparer]] {
this.push[element];
}
};
var array = [{ name: "tom", text: "tasty" }];
var element = { name: "tom", text: "tasty" };
array.pushIfNotExist[element, function[e] {
return e.name === element.name && e.text === element.text;
}];
7 + // check if an element exists in array using a comparer function
// comparer : function[currentElement]
Array.prototype.inArray = function[comparer] {
for[var i=0; i < this.length; i++] {
if[comparer[this[i]]] return true;
}
return false;
};
// adds an element to the array if it does not already exist using a comparer
// function
Array.prototype.pushIfNotExist = function[element, comparer] {
if [!this.inArray[comparer]] {
this.push[element];
}
};
var array = [{ name: "tom", text: "tasty" }];
var element = { name: "tom", text: "tasty" };
array.pushIfNotExist[element, function[e] {
return e.name === element.name && e.text === element.text;
}];
8 là một cách tiếp cận đơn giản. Bắt đầu với việc biến mảng của bạn thành một chuỗi:9Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Sau đó, đối với một loạt các cặp giá trị thuộc tính bạn có thể sử dụng:
0Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
Tìm toàn bộ một đối tượng đơn giản hơn:
1Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
Đã trả lời ngày 20 tháng 11 năm 2017 lúc 2:32Nov 20, 2017 at 2:32
Moshefnordmoshefnordmoshefnord
1582 Huy hiệu bạc8 Huy hiệu đồng2 silver badges8 bronze badges
Trong trường hợp bất kỳ ai có các yêu cầu ít phức tạp hơn, đây là bản chuyển thể của tôi về câu trả lời cho một mảng chuỗi đơn giản:
2Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
CẬP NHẬT: Thay thế Indexof và Cắt bằng các lựa chọn thay thế jQuery cho khả năng tương thích IE8
Đã trả lời ngày 25 tháng 3 năm 2015 lúc 17:16Mar 25, 2015 at 17:16
Joatmonthe JoatmonTHE JOATMON
17.1K35 Huy hiệu vàng112 Huy hiệu bạc205 Huy hiệu đồng35 gold badges112 silver badges205 bronze badges
1
Trong trường hợp bạn cần một cái gì đó đơn giản mà không muốn mở rộng nguyên mẫu mảng:
3Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
Đã trả lời ngày 15 tháng 1 năm 2016 lúc 6:37Jan 15, 2016 at 6:37
docta_faustusdocta_faustusdocta_faustus
2.0733 huy hiệu vàng27 Huy hiệu bạc42 Huy hiệu đồng3 gold badges27 silver badges42 bronze badges
Tôi đã sử dụng bản đồ và giảm để làm điều này trong trường hợp bạn muốn tìm kiếm bởi một thuộc tính cụ thể của một đối tượng, hữu ích vì việc làm cho sự bình đẳng đối tượng trực tiếp thường sẽ thất bại.
4Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
Đã trả lời ngày 23 tháng 8 năm 2016 lúc 17:51Aug 23, 2016 at 17:51
VivalapandAvivalApandaVivaLaPanda
8017 Huy hiệu bạc24 Huy hiệu đồng7 silver badges24 bronze badges
Bạn có thể sử dụng phương thức FindIndex với chức năng gọi lại và tham số "This" của nó.
Lưu ý: Các trình duyệt cũ không biết FindIndex nhưng có sẵn một polyfill.
Mã mẫu [cẩn thận rằng trong câu hỏi ban đầu, một đối tượng mới chỉ được đẩy nếu cả hai dữ liệu của nó không ở trong các đối tượng được đẩy trước Previoulsy]:
5Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
Đã trả lời ngày 7 tháng 11 năm 2016 lúc 14:40Nov 7, 2016 at 14:40
Simon Hisimon HiSimon Hi
2.7681 Huy hiệu vàng15 Huy hiệu bạc16 Huy hiệu đồng1 gold badge15 silver badges16 bronze badges
Tôi đoán tôi đã quá muộn để trả lời ở đây tuy nhiên đây là những gì cuối cùng tôi đã đưa ra cho một người quản lý thư mà tôi đã viết. Tác phẩm đó là tất cả những gì tôi cần.
6Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
7Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
Đã trả lời ngày 1 tháng 7 năm 2019 lúc 8:22Jul 1, 2019 at 8:22
A là mảng các đối tượng bạn có
8Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
Đã trả lời ngày 12 tháng 10 năm 2019 lúc 14:04Oct 12, 2019 at 14:04
Loại bỏ các bản sao sau khi đẩy
Nếu bạn đã có một mảng có chứa các bản sao, hãy chuyển đổi mảng các đối tượng thành một mảng các chuỗi và sau đó sử dụng hàm
// check if an element exists in array using a comparer function
// comparer : function[currentElement]
Array.prototype.inArray = function[comparer] {
for[var i=0; i < this.length; i++] {
if[comparer[this[i]]] return true;
}
return false;
};
// adds an element to the array if it does not already exist using a comparer
// function
Array.prototype.pushIfNotExist = function[element, comparer] {
if [!this.inArray[comparer]] {
this.push[element];
}
};
var array = [{ name: "tom", text: "tasty" }];
var element = { name: "tom", text: "tasty" };
array.pushIfNotExist[element, function[e] {
return e.name === element.name && e.text === element.text;
}];
9 để loại bỏ các bản sao:9Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
Kiểm tra trước khi đẩy
Nếu bạn không có bản sao cho đến nay và bạn muốn kiểm tra các bản sao trước khi đẩy một yếu tố mới:
var newItem = "NEW_ITEM_TO_ARRAY";
var array = ["OLD_ITEM_1", "OLD_ITEM_2"];
array.indexOf[newItem] === -1 ? array.push[newItem] : console.log["This item already exists"];
console.log[array]
0Đã trả lời ngày 27 tháng 10 năm 2020 lúc 10:15Oct 27, 2020 at 10:15
3
Tôi muốn sử dụng JS bản địa
var cleanArray = $.unique[clutteredArray];
0 ngay cả trong JQ Env Docs: W3S var cleanArray = $.unique[clutteredArray];
1 hoặc MDN var cleanArray = $.unique[clutteredArray];
1Docs: w3s
var cleanArray = $.unique[clutteredArray];
1 or
mdn var cleanArray = $.unique[clutteredArray];
1var newItem = "NEW_ITEM_TO_ARRAY";
var array = ["OLD_ITEM_1", "OLD_ITEM_2"];
array.indexOf[newItem] === -1 ? array.push[newItem] : console.log["This item already exists"];
console.log[array]
1Đã trả lời ngày 4 tháng 2 lúc 22:07Feb 4 at 22:07
0
Câu hỏi là một chút cũ, nhưng đó là lựa chọn của tôi:
var newItem = "NEW_ITEM_TO_ARRAY";
var array = ["OLD_ITEM_1", "OLD_ITEM_2"];
array.indexOf[newItem] === -1 ? array.push[newItem] : console.log["This item already exists"];
console.log[array]
2Đã trả lời ngày 25 tháng 6 năm 2021 lúc 8:07Jun 25, 2021 at 8:07
GoelandmangoelandmanGoelandMan
Huy hiệu đồng bạc 311 Huy hiệu đồng1 silver badge4 bronze badges
Ví dụ ngắn:
var newItem = "NEW_ITEM_TO_ARRAY";
var array = ["OLD_ITEM_1", "OLD_ITEM_2"];
array.indexOf[newItem] === -1 ? array.push[newItem] : console.log["This item already exists"];
console.log[array]
3Đã trả lời ngày 26 tháng 7 năm 2018 lúc 16:59Jul 26, 2018 at 16:59
GigolandgigolandGigoland
9619 Huy hiệu bạc10 Huy hiệu đồng9 silver badges10 bronze badges
1
Ở đây bạn có một cách để thực hiện nó trong một dòng cho hai mảng:
var newItem = "NEW_ITEM_TO_ARRAY";
var array = ["OLD_ITEM_1", "OLD_ITEM_2"];
array.indexOf[newItem] === -1 ? array.push[newItem] : console.log["This item already exists"];
console.log[array]
4Đã trả lời ngày 29 tháng 3 năm 2020 lúc 15:48Mar 29, 2020 at 15:48
JöckerjöckerJöcker
4.0711 Huy hiệu vàng34 Huy hiệu bạc37 Huy hiệu đồng1 gold badge34 silver badges37 bronze badges
var newItem = "NEW_ITEM_TO_ARRAY";
var array = ["OLD_ITEM_1", "OLD_ITEM_2"];
array.indexOf[newItem] === -1 ? array.push[newItem] : console.log["This item already exists"];
console.log[array]
5Đã trả lời ngày 9 tháng 7 năm 2020 lúc 8:14Jul 9, 2020 at 8:14
1
Tôi đã tìm ra vấn đề này và tôi đã tạo ra một nguyên mẫu đơn giản, sử dụng nó nếu bạn thích nó
var newItem = "NEW_ITEM_TO_ARRAY";
var array = ["OLD_ITEM_1", "OLD_ITEM_2"];
array.indexOf[newItem] === -1 ? array.push[newItem] : console.log["This item already exists"];
console.log[array]
6Đã trả lời ngày 13 tháng 10 năm 2021 lúc 17:50Oct 13, 2021 at 17:50
EboubakereboubakerEboubaker
4905 Huy hiệu bạc15 Huy hiệu Đồng5 silver badges15 bronze badges
Nếu dự án của bạn bao gồm lodash, nó sẽ đơn giản bằng cách sử dụng phương pháp
var cleanArray = $.unique[clutteredArray];
3var newItem = "NEW_ITEM_TO_ARRAY";
var array = ["OLD_ITEM_1", "OLD_ITEM_2"];
array.indexOf[newItem] === -1 ? array.push[newItem] : console.log["This item already exists"];
console.log[array]
7Đã trả lời ngày 3 tháng 8 lúc 15:20Aug 3 at 15:20
Hemerson Varelahemerson VarelaHemerson Varela
22.5K15 Huy hiệu vàng67 Huy hiệu bạc69 Huy hiệu đồng15 gold badges67 silver badges69 bronze badges
Đây là hoạt động func để so sánh đối tượng. Trong một số trường hợp, bạn có thể có rất nhiều trường để so sánh. Chỉ cần lặp lại mảng và gọi chức năng này với một mục hiện có và mục mới.
var newItem = "NEW_ITEM_TO_ARRAY";
var array = ["OLD_ITEM_1", "OLD_ITEM_2"];
array.indexOf[newItem] === -1 ? array.push[newItem] : console.log["This item already exists"];
console.log[array]
8Đã trả lời ngày 18 tháng 5 năm 2017 lúc 5:11May 18, 2017 at 5:11
1
Bạn có thể sử dụng jQuery grep và đẩy nếu không có kết quả: //api.jquery.com/jquery.grep/
Về cơ bản, đó là giải pháp tương tự như trong giải pháp "mở rộng nguyên mẫu", nhưng không mở rộng [hoặc gây ô nhiễm] nguyên mẫu.
Đã trả lời ngày 24 tháng 7 năm 2013 lúc 12:59Jul 24, 2013 at 12:59
Jgroenenjgroenenjgroenen
1.3081 Huy hiệu vàng8 Huy hiệu bạc13 Huy hiệu đồng1 gold badge8 silver badges13 bronze badges
Bạn có thể kiểm tra mảng bằng foreach và sau đó bật vật phẩm nếu nó tồn tại khác thêm mục mới ...
Mẫu NewItemValue & Subreffields là khóa, các cặp giá trị
var newItem = "NEW_ITEM_TO_ARRAY";
var array = ["OLD_ITEM_1", "OLD_ITEM_2"];
array.indexOf[newItem] === -1 ? array.push[newItem] : console.log["This item already exists"];
console.log[array]
9Đã trả lời ngày 13 tháng 7 năm 2016 lúc 17:01Jul 13, 2016 at 17:01
TarantaranTaran
2.59524 Huy hiệu bạc22 Huy hiệu đồng24 silver badges22 bronze badges
0