Abhirock
Abhirock

Reputation: 445

Getting a array as a result when the result should be an Map?

I am trying to use MAP into node.js program and I am getting final result however its in array but I need in map. I have write some code to use map but its not working as expected.

Please find below program

function CreateProduceMVPRateAsset(data, callback) {
    var ProducePRICE = {};

    var MVPPRICE = [];
    var MVPPRICE_BS = {};
    var MVPPRICE_LB = {};
    const walletPath = path.join(process.cwd(), 'wallet');
    const wallet = new FileSystemWallet(walletPath);
    console.log(`Wallet path: ${walletPath}`);
    console.log('Data', data);
    console.log('Username', data.username);
    var PRODUCENAME = data.PRODUCE;

    var COUNTRY = data.COUNTRY;
    var STATE = data.STATE;
    var MVPRATES = data.MVPRATES;
    console.log('MVPRATERATE', MVPRATES);
    // here I need help
    const MVPRATE = new Map(MVPRATES);

    for (const [k, v] of MVPRATE.entries()) {
        console.log('Inside map', k, v);
        MVPPRICE = v.RATE; // should go in  MVPPRICE

        var Value = MVPPRICE[0].value; // want to get first element value from MVPPRICE array
        console.log('Value', Value);

        var value_lb = Value / 40;
        console.log('value_lb', value_lb);

        value_lb = Number(value_lb.toFixed(4));

        console.log('If the value of BS provided controller come here');

        MVPPRICE_LB.Value = value_lb;
        MVPPRICE_LB.QuantityUnit = 'LB';
        MVPPRICE_LB.uidisplay = false;
        MVPPRICE_LB.CurrencyUnit = 'USD';

        MVPPRICE.push(MVPPRICE_LB);
        ProducePRICE.MVPPRICE = MVPPRICE;

        ProducePRICE.PRODUCENAME = PRODUCENAME;
        ProducePRICE.STATE = STATE;
        ProducePRICE.COUNTRY = COUNTRY;
        console.log('ProducePRICE', ProducePRICE); // whatever result getting it should be map however getting in array.
    }
}

JSON structure which I am sending using postman

{
"username": "admin2",
  "PRODUCE": "Apple",
  "STATE": "MI",
  "COUNTRY": "US",
  "MVPRATES": {
    "fuji": {
      "VARIETY": "fuji",
      "RATE": [
        {
          "UNIT": "Bussel",
          "CURRENCY": "USD",
          "VALUE": 10.25,
          "UIDISPLAY": true
        }
      ]
    },
    "gala": {
      "VARIETY": "gala",
      "RATE": [
        {
          "UNIT": "Bussel",
          "CURRENCY": "USD",
          "VALUE": 10.25,
          "UIDISPLAY": true
        }
      ]
    }
  }
}

Output which I am getting:

enter image description here

Upvotes: 0

Views: 76

Answers (1)

pinkwaffles
pinkwaffles

Reputation: 463

@SudhakarRS is right, you're getting your MAP back, you're just getting your values in an array for MVPRATE, and it almost looks like that was your intention, seeing you push here:

MVPPRICE.push(MVPPRICE_LB);

I mentioned in the comments, but I think what's happening is the Map doesn't apply recursively, so when it sees MVPPRICE, it's treating your value array, as the value instead of part of the map.

I think what you're trying to do can be solved by this:

MVPPRICE.concat(MVPPRICE_LB);
ProducePRICE.MVPPRICE = new Map(MVPPRICE)

Upvotes: 1

Related Questions