Javascript 在单独的数组中连接元素
我想把两个阵列连接在一起。 我有两个数组,希望它们的元素是“组合”的 假设我有一台扫描仪。 它为数组1中的每个元素提供了一个值(比如US$)。 如果每个元素的值小于或等于array2中的值,我希望它返回一些内容(比如字符串“this product is sally for cheap!”) 我只希望它将数组中的一个项目链接到同一位置的另一个项目 两个阵列(简要如上所述): 因此,741将连接到15451到4185418,54188到488,等等 在它们被“链接”之后,我将能够看到array1中元素的值(通过值,我指的是我的扫描仪为每个元素找到的值)是否等于/小于array2中该元素的值 例如,将741放入扫描仪返回16。 这不少于15,所以不会返回任何内容。 -然而,将451放入扫描仪返回417。这小于418,因此它将返回“此产品售价便宜!”Javascript 在单独的数组中连接元素,javascript,arrays,Javascript,Arrays,我想把两个阵列连接在一起。 我有两个数组,希望它们的元素是“组合”的 假设我有一台扫描仪。 它为数组1中的每个元素提供了一个值(比如US$)。 如果每个元素的值小于或等于array2中的值,我希望它返回一些内容(比如字符串“this product is sally for cheap!”) 我只希望它将数组中的一个项目链接到同一位置的另一个项目 两个阵列(简要如上所述): 因此,741将连接到15451到4185418,54188到488,等等 在它们被“链接”之后,我将能够看到array1中
我希望这是有意义的。我将创建一个对象并将其放在单个数组中
var my Obj = {no1:741, no2:15, text:'some text'}
这将使您无需扫描仪即可构建阵列,因为它已存储在“已扫描”状态中您可以使用javascript对象,其工作原理类似于字典(或hashmap)
在这种排列中,数组1包含字典的
键
,数组2包含字典的值。这里有一个简单的解决方案,只需使用.indexOf()
在第一个数组中查找产品代码,然后使用该索引在第二个数组中查找比较价格:
Product Number: <input id="testVal" type="text" value="741"><br>
Test Price: <input id="priceVal" type="text" value=15><br>
<button id="go">Lookup</button><br><br>
<div id="result"></div>
document.getElementById("go").addEventListener("click", function() {
var prod = +document.getElementById("testVal").value;
var price = +document.getElementById("priceVal").value;
var result = lookupValue(prod, price);
document.getElementById("result").innerHTML = result;
});
function lookupValue(prod, price) {
var productCodes = [741, 451, 54188, 5847, 5418, 54944, 310, 541, 7451, 10211, 113, 9115, 62, 2841, 52482481, 24];
var prices = [15, 418, 488, 130000, 8482, 55, 16, 14, 2546, 651, 4521, 11, 54, 659, 542, 1152];
var index;
if (!prod || !price) {
return "Please Enter a Product Number and Price";
}
if ((index = productCodes.indexOf(prod)) >= 0) {
if (index < prices.length && price < prices[index]) {
return "This product is selling for cheap!";
} else {
return "This product is not a bargain.";
}
} else {
return "Product not found for comparison";
}
}
产品编号:
测试价格:
查找
document.getElementById(“go”).addEventListener(“单击”,函数(){
var prod=+document.getElementById(“testVal”).value;
var price=+document.getElementById(“priceVal”).value;
var结果=查找值(产品、价格);
document.getElementById(“结果”).innerHTML=result;
});
函数查找值(产品、价格){
var productCodes=[741451541885847541854944105417451102111139115 62284152482481,24];
var价格=[154184881300008482,55,16,1425466514521,11,546595421152];
var指数;
如果(!prod | |!price){
return“请输入产品编号和价格”;
}
如果((index=productCodes.indexOf(prod))>=0){
if(指数<价格.长度和价格<价格[指数]){
return“此产品售价便宜!”;
}否则{
return“此产品不便宜。”;
}
}否则{
返回“未找到产品进行比较”;
}
}
工作演示:
对于这么多的产品代码,只需使用.indexOf()
在第一个数组中查找匹配项就完全没有问题了。但是,如果您有很多产品代码,那么将它们索引到一个对象中进行直接查找会快得多。@zerkms我是Javascript新手,并不真正了解.indexOf()。您愿意解释一下吗?@user3810560-我建议您熟悉在哪里可以查找任何javascript方法或任何javascript对象类型,看看它有哪些方法。这是非常简单和充分的描述。如果你学习了好的参考资料,那么你应该能够自己找到更多信息的答案。
var myObj = {};
array1.forEach(function(item, i) {
myObj[item] = array2[i];
});
alert(myObj[741]);
>>> 15
Product Number: <input id="testVal" type="text" value="741"><br>
Test Price: <input id="priceVal" type="text" value=15><br>
<button id="go">Lookup</button><br><br>
<div id="result"></div>
document.getElementById("go").addEventListener("click", function() {
var prod = +document.getElementById("testVal").value;
var price = +document.getElementById("priceVal").value;
var result = lookupValue(prod, price);
document.getElementById("result").innerHTML = result;
});
function lookupValue(prod, price) {
var productCodes = [741, 451, 54188, 5847, 5418, 54944, 310, 541, 7451, 10211, 113, 9115, 62, 2841, 52482481, 24];
var prices = [15, 418, 488, 130000, 8482, 55, 16, 14, 2546, 651, 4521, 11, 54, 659, 542, 1152];
var index;
if (!prod || !price) {
return "Please Enter a Product Number and Price";
}
if ((index = productCodes.indexOf(prod)) >= 0) {
if (index < prices.length && price < prices[index]) {
return "This product is selling for cheap!";
} else {
return "This product is not a bargain.";
}
} else {
return "Product not found for comparison";
}
}