![](/img/trans.png)
[英]Given a nested object I need to read values and display output in array
[英]Read array of objects and output values by given conditions in JavaScript
这是问题所在:
客户想购买两台同色的白色或黑色同名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);
书面代码可以找到最好的报价,但是有人可以说出为什么输出中的acer
重复吗?如何修复?
非常感谢您的帮助,
您遍历Collors
。 对于acer
, black
和white
都>2
。 因此,检查black and white
匹配的第一个规则是一次为black
,一次为white
。
一个解决办法是不遍历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"));
解决问题的代码以在控制台中获取所需的输出。
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.