skip to Main Content

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


  1. As soon as the else block executes, the for loop will not make any other iterations, as you exit it with that return. You cannot know whether you have not found it before you have made all iterations of the for loop, so that else ... return should not be inside your loop.

    You could do something like this (with minimal changes):

    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);
            return; // You probably want to stop looking further
        }
    }
    // If we get here, we didn't find it
    conn.reply(m.chat, `Sorry. Could not find any product code of ${kod},   Perhaps a typo?`, m);
    

    I would suggest using more modern JavaScript features and use the some array method (without logging this time):

    const found = data.some(obj => obj.buyer_sku_code === kod);
    const msg = found ? "Passed" : `Sorry. Could not find any product code of ${kod}, perhaps a typo?`;
    conn.reply(m.chat, msg, m);
    
    Login or Signup to reply.
  2. iteration.

    let arrOfObjects = [
        {
          "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": "-"
        } 
    ]
    
    arrOfObjects.forEach(function(obj){
    console.log(obj["buyer_sku_code"])
    })
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search