[英]Read array of objects and output values by given conditions in JavaScript
Here is the problem: 这是问题所在:
Client wants to buy 2 same name PCs both color white or black with the total price less than 1600. Write a JavaScript program that would read given array var pcs
and would find him the best offers. 客户想购买两台同色的白色或黑色同名PC,总价格低于1600。编写一个JavaScript程序,该程序可以读取给定的阵列
var pcs
,并找到最优惠的价格。
var pcs = [ { "model":"lenovo", "price":1234, "color":{"red":1,"green":2} }, { "model":"hp", "price":800, "color":{"black":2,"yellow":0} }, { "model":"toshiba", "price":256, "color":{"mėlyna":3,"green":1} }, { "model":"dell", "price":697, "color":{"black":1,"white":2} }, { "model":"acer", "price":620, "color":{"black":4,"white":2} }, { "model":"apple", "price":2560, "color":{"white":3,"black":1} }, { "model":"asus", "price":1001, "color":{"black":2,"yellow":3} } ], PC, Collors, offer = "Offers: " + "\\n"; for (var i = 0; i < pcs.length; i++) { PC = pcs[i]; Collors = Object.keys(PC.color); if ((((PC.price) * 2) <= 1600) && (PC.color.black >= 2 || PC.color.white >= 2)) { for ( var j = 0; j < Collors.length; j++) { if ((PC.color.black >= 2) && (PC.color.white >= 2)) { offer += "\\n" + "model: " + PC.model + "\\n" + "price: " + (PC.price) * 2 + "\\n" + "Collors: " + Collors[0] + " and " + Collors[1] + "\\n"; } else if (((PC.color.black >= 2) && (Collors[j] === "black"))) { offer += "\\n" + "model: " + PC.model + "\\n" + "price: " + (PC.price) * 2 + "\\n" + "Collors: " + Collors[j] + "\\n"; } else if (((PC.color.white >= 2) && (Collors[j] === "white"))) { offer += "\\n" + "model: " + PC.model + "\\n" + "price: " + (PC.price) * 2 + "\\n" + "Collors: " + Collors[j] + "\\n"; } } } } console.log(offer);
Written code find the best offers, but could someone tell why acer
in the output duplicates itself and how is possible to repair it? 书面代码可以找到最好的报价,但是有人可以说出为什么输出中的
acer
重复吗?如何修复?
Many thanks for all help, 非常感谢您的帮助,
You loop over Collors
. 您遍历
Collors
。 For acer
both black
and white
have >2
. 对于
acer
, black
和white
都>2
。 So the first rule that checks black and white
matches once for black
and once for white
. 因此,检查
black and white
匹配的第一个规则是一次为black
,一次为white
。
A solution is to not loop over Collors
at all but check black
and white
separately directly: 一个解决办法是不遍历
Collors
在所有但检查black
和white
分别直接:
var pcs = [ { "model":"lenovo", "price":1234, "color":{"red":1,"green":2} }, { "model":"hp", "price":800, "color":{"black":2,"yellow":0} }, { "model":"toshiba", "price":256, "color":{"melyna":3,"green":1} }, { "model":"dell", "price":697, "color":{"black":1,"white":2} }, { "model":"acer", "price":620, "color":{"black":4,"white":2} }, { "model":"apple", "price":2560, "color":{"white":3,"black":1} }, { "model":"asus", "price":1001, "color":{"black":2,"yellow":3} } ], PC, Collors, offer = [], colors; for (var i = 0; i < pcs.length; i++) { colors = []; PC = pcs[i]; if (PC.color.black >= 2) { colors.push("black"); } if (PC.color.white >= 2) { colors.push("white"); } if (PC.price * 2 <= 1600 && colors.length > 0) { offer.push({ "model": PC.model, "total price": PC.price * 2, "color": colors }); } } console.log(JSON.stringify(offer, undefined, "\\t"));
Code to solve problem to get required output in console. 解决问题的代码以在控制台中获取所需的输出。
var pcs = [ { "model":"lenovo", "price":1234, "color":{"red":1,"green":2} }, { "model":"hp", "price":800, "color":{"black":2,"yellow":0} }, { "model":"toshiba", "price":256, "color":{"mėlyna":3,"green":1} }, { "model":"dell", "price":697, "color":{"black":1,"white":2} }, { "model":"acer", "price":620, "color":{"black":4,"white":2} }, { "model":"apple", "price":2560, "color":{"white":3,"black":1} }, { "model":"asus", "price":1001, "color":{"black":2,"yellow":3} } ]; for (var i=0; i < pcs.length; i++) { if((pcs[i].color.black >=2 || pcs[i].color.white >=2) && pcs[i].price *2 <=1600){ //console.log (pcs[i]); var colors = Object.keys(pcs[i].color), color = ''; for(var j = 0; j < colors.length; j++){ if ((pcs[i].color.black >= 2 && colors[j] === "black") || (pcs[i].color.white >= 2 && colors[j] === "white" )) { if (color.length > 0) { color += ' and ' + colors[j]; } else { color = colors[j]; } } } console.log('Model: ' + pcs[i].model + '\\nPrice: ' + pcs[i].price*2 + '\\nSpalvos: ' + color); } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.