从对象获取值始终返回未定义

时间:2019-11-02 17:37:39

标签: javascript json typescript object

我有下面的对象,我已将其打印为json。

{"36":92,"38":93,"40":98,"41":16,"42":13,"43":38,"44":64,"45":94,"46":85,"47":100,"48":21,"49":33,"50":59,"51":79,"52":81,"53":14,"54":60,"55":103,"56":44,"57":24,"58":28,"59":65,"60":99,"61":23,"62":2,"63":31,"64":74,"65":110,"66":97,
"67":17,"68":43,"69":72,"70":95,"71":111,"72":56,"73":4,"74":46,"75":86,"76":114,"77":5,"78":20,"79":57,"80":104,"81":75,"82":18,"83":66,"84":77,"85":19,"86":49,"87":36,"88":91,"89":25,"90":70,"91":107,"92":55,"93":108,"94":10,"95":48,
"96":54,"97":22,"98":9,"99":42,"100":58,"101":29,"102":82,"103":102,"104":78,"105":34,"106":52,"107":50,"108":0,"109":83,"110":89,"111":12,"112":68,"113":96,"114":88,"115":47,"116":6,"117":109,"118":30,"119":39,"120":73,"121":63,"122":
69,"123":87,"124":53,"125":61,"126":106,"127":15,"128":26,"129":71,"130":37,"131":51,"132":7,"133":1,"134":80,"135":76,"136":101,"137":8,"138":105,"139":84,"140":113,"141":40,"142":3,"143":35,"144":62,"145":27,"146":67,"147":45,"148":1
12,"149":90,"150":11,"151":41,"152":32}

但是获得说“ 149”的值总是返回未定义。我不确定是什么原因引起的。

我需要一个映射,其中产品ID映射到其在数组中的位置

const productsInFormat: ProductInFormatImpl[] = [];
const productMap = {};
for (const product of products) {
  const productIndex = productMap[product.productId];
  if (!(productIndex === undefined || productIndex === null)) {
    ("DO SOmething here");
  } else {
    const arrayLength = productsInFormat.push(productInFormat);
    productMap[product.productId] = arrayLength - 1;
  }
}

0 个答案:

没有答案