I am a new node js programmer, so help is much appreciated, I am trying to log all the buyer_sku_code but it only logs one of them which is only the "WDP" array
this is the JSON
[
{
"product_name": "WDP",
"brand": "ml",
"price": 18245,
"buyer_sku_code": "mlgwdp",
"buyer_product_status": true,
"seller_product_status": true,
"unlimited_stock": true,
"stock": 0,
"multi": true,
"start_cut_off": "23:45",
"end_cut_off": "00:15",
"desc": "-"
},
{
"product_name": "86 Diamonds",
"brand": "ml",
"price": 16135,
"buyer_sku_code": "mlg86",
"buyer_product_status": true,
"seller_product_status": true,
"unlimited_stock": true,
"stock": 0,
"multi": true,
"start_cut_off": "23:45",
"end_cut_off": "00:15",
"desc": "-"
},
{
"product_name": "172 Diamonds",
"brand": "ml",
"price": 31992,
"buyer_sku_code": "mlg172",
"buyer_product_status": true,
"seller_product_status": true,
"unlimited_stock": true,
"stock": 0,
"multi": true,
"start_cut_off": "23:45",
"end_cut_off": "00:15",
"desc": "-"
},
{
"product_name": "257 Diamonds",
"brand": "ml",
"price": 47603,
"buyer_sku_code": "mlg257",
"buyer_product_status": true,
"seller_product_status": true,
"unlimited_stock": true,
"stock": 0,
"multi": true,
"start_cut_off": "23:45",
"end_cut_off": "00:15",
"desc": "-"
},
{
"product_name": "344 Diamonds",
"brand": "ml",
"price": 64969,
"buyer_sku_code": "mlg344",
"buyer_product_status": true,
"seller_product_status": true,
"unlimited_stock": true,
"stock": 0,
"multi": true,
"start_cut_off": "23:45",
"end_cut_off": "00:15",
"desc": "-"
}
]
this is what i wrote in the nodejs script
fs.readFile('./custom-raeonn/digiflazz/prepaidData.json', 'utf8', function (err,data) {
data = JSON.parse(data);
for(var i = 0; i < data.length; i++) {
console.log(data[i].buyer_sku_code);
if (data[i].buyer_sku_code== kod) {
conn.reply(m.chat,`Passed`,m)
} else { return conn.reply(m.chat,`Sorry. Could not find any product code of ${kod}, Perhaps a typo?`,m) }
}
});
It logs only one which is "mlgwdp" but not the others
Ive searched everywhere for the answer but none seems to be working. Im not really good at explaining but i am expecting to log all the "buyer_sku_code" codes
2
Answers
As soon as the
else
block executes, thefor
loop will not make any other iterations, as you exit it with thatreturn
. You cannot know whether you have not found it before you have made all iterations of thefor
loop, so thatelse ... return
should not be inside your loop.You could do something like this (with minimal changes):
I would suggest using more modern JavaScript features and use the
some
array method (without logging this time):iteration.