在Js中工作时,我需要检查数组中的值是否存在&如果它存在对用户显示错误,&如果没有把它推入阵列。 这是我的代码片段。
<html>
<body>
<label>Enter an New item to add in Stock</label>
<br> </br>
<input type="text" name=" itemName" id="addItemInStock">
<br></br>
<p id="errorMsg"></p>
<button onclick="addToStock()">Add</button>
<p id="showList"></p>
<select id="showInDropDown">
<option disabled selected style="display: block;">Stock Items</option>
</select>
<script>
var fruitsfromLS = localStorage.getItem("fruits");
var fruits = fruitsfromLS ? JSON.parse(fruitsfromLS) : ["Banana", "Orange", "Apple", "Mango"];
//document.getElementById("showList").innerHTML = fruits;
var newItem = document.getElementById("addItemInStock");
function addToStock() {
if ((newItem.value) === "") {
document.getElementById("errorMsg").innerHTML = "Blank item cannot be added!!";
document.getElementById("errorMsg").style.display = "block";
} else if ((newItem.value) === fruits[i].value)) {
document.getElementById("errorMsg").innerHTML = "aLREADY IN sTOCK!";
document.getElementById("errorMsg").style.display = "block";
} else {
document.getElementById("errorMsg").style.display = "none";
fruits.push(newItem.value);
localStorage.setItem("fruits", JSON.stringify(fruits));
clearAndShow();
}
fillSelect();
}
答案 0 :(得分:1)
简单使用此检查:
!!~array.indexOf(element)
如果element在数组中,则返回true;如果element不存在,则返回false。
您还可以使用自己的方法(即包含)扩展Array类型,以便在代码中使用它,如下所示:
Array.prototype.contains = Array.prototype.contains || function(element) {
return !!~this.indexOf(element);
};
let a = [ -2, -1, 0, 1, 2, 3, 4, 5 ]; // an example of array
console.log( a.contains(-1) ); // true
console.log( a.contains(10) ); // false
答案 1 :(得分:0)
请添加本地存储代码和fillStcok功能以获得完整功能。以下代码段用于检查重复的
var fruits = ["Banana", "Orange", "Apple", "Mango"];
//document.getElementById("showList").innerHTML = fruits;
var newItem = document.getElementById("addItemInStock");
function addToStock() {
if (newItem.value === "") {
document.getElementById("errorMsg").innerHTML = "Blank item cannot be added!!";
document.getElementById("errorMsg").style.display = "block";
} else if (fruits.indexOf(newItem.value) !== -1) {
document.getElementById("errorMsg").innerHTML = "ALREADY IN STOCK!";
document.getElementById("errorMsg").style.display = "block";
} else {
document.getElementById("errorMsg").style.display = "none";
fruits.push(newItem.value);
}
}
<label>Enter an New item to add in Stock</label>
<br> </br>
<input type="text" name=" itemName" id="addItemInStock">
<br></br>
<p id="errorMsg"></p>
<button onclick="addToStock()">Add</button>
<p id="showList"></p>
<select id="showInDropDown">
<option disabled selected style="display: block;">Stock Items</option>
</select>
答案 2 :(得分:0)
您可以始终使用Array.prototype.find()方法,只需小扩展。它快速而同步。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
var findFunc = function(searchVal, element, index, arr) {
return searchVal == element
}
var freshOne = fruits.find(findFunc.bind(undefined, newItem.value)) == undefined;
if (freshOne) {
fruits.push(newItem.value)
} else {
console.log('Already there');
}