gpt4 book ai didi

javascript - 无法在 Node.js 中使用 MAP 数据结构

转载 作者:行者123 更新时间:2023-12-02 22:49:02 27 4
gpt4 key购买 nike

我在 Node.js 中的 map 程序中遇到一些问题。我正在尝试在 node.js 程序中使用 MAP,但没有获得值(value)。我已经编写了一些代码来使用 map ,但我认为它不起作用,因为它没有进入循环来获取键值。

请找到以下程序

  async CreateProduceMVPRateAsset(data, callback) {

var ProducePRICE = {};

var MVPPRICE =[];
var MVPPRICE_BS ={};
var MVPPRICE_LB ={};
//var ASKINGPRICE= {}// put all the things which need to go to blockchain
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
console.log('PRODUCENAME', PRODUCENAME);

var COUNTRY = data.COUNTRY;
console.log('COUNTRY', COUNTRY);
var STATE = data.STATE;
console.log('STATE', STATE);


var MVPRATES = data.MVPRATES;

console.log('MVPRATERATE', MVPRATES);

const MVPRATE = new Map(MVPRATES);

// program could not go inside the for loop
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

console.log('MVPPRICE',MVPPRICE);
console.log('ProducePRICE',ProducePRICE);

// mvpprice.totalPrice = totalPrice;
console.log('for pricing',totalPrice);

// ProducePRICE.VARIETY = VARIETY;
ProducePRICE.PRODUCENAME = PRODUCENAME;
ProducePRICE.STATE = STATE;
ProducePRICE.COUNTRY = COUNTRY;

}

我使用 postman 发送的 JSON 结构

{
"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
}
]
}
}
}

我得到的输出: enter image description here

最佳答案

似乎您可能想要执行以下操作,这将创建一个包含两个条目的 Map:fujigala

const 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
}]
}
}
const MVPRATE = new Map(Object.entries(MVPRATES));
for (const [k, v] of MVPRATE.entries()) {
console.log("Inside map", k, v)
}

尽管如此,对 Map 的需求仍不清楚,因为您可以通过以下方式实现上述目标

const 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
}]
}
}
for (const [k, v] of Object.entries(MVPRATES)) {
console.log("Inside map", k, v)
}

关于javascript - 无法在 Node.js 中使用 MAP 数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58266437/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com