关于在JavaScript中映射对象的问题

时间:2017-12-11 23:53:47

标签: javascript

请您看一下这个演示,让我知道为什么我无法在数组/对象中映射显示名称?

var data = [{
    "displayFieldName": "",
    "fieldAliases": {
      "OBJECTID": "OBJECTID"
    },
    "fields": [{
      "name": "OBJECTID",
      "type": "esriFieldTypeOID",
      "alias": "OBJECTID"
    }],
    "features": [{
        "attributes": {
          "OBJECTID": 649
        }
      },
      {
        "attributes": {
          "OBJECTID": 665
        }
      },
      {
        "attributes": {
          "OBJECTID": 762
        }
      }
    ]
  },
  {
    "displayFieldName": "",
    "fieldAliases": {
      "display": "display",
      "OBJECTID": "OBJECTID"
    },
    "geometryType": "esriGeometryPoint",
    "spatialReference": {
      "wkid": 4326,
      "latestWkid": 4326
    },
    "fields": [{
        "name": "display",
        "type": "esriFieldTypeString",
        "alias": "display",
        "length": 50
      },
      {
        "name": "OBJECTID",
        "type": "esriFieldTypeOID",
        "alias": "OBJECTID"
      }
    ],
    "features": [{
        "attributes": {
          "display": "Transformer",
          "OBJECTID": 1537
        },
        "geometry": {
          "x": -88.17602806699995,
          "y": 41.78431233100008
        }
      },
      {
        "attributes": {
          "display": "Transformer",
          "OBJECTID": 1591
        },
        "geometry": {
          "x": -88.17546081099994,
          "y": 41.783341919000065
        }
      }
    ]
  },
  {
    "displayFieldName": "",
    "fieldAliases": {
      "display": "display",
      "OBJECTID": "OBJECTID"
    },
    "geometryType": "esriGeometryPoint",
    "spatialReference": {
      "wkid": 4326,
      "latestWkid": 4326
    },
    "fields": [{
        "name": "display",
        "type": "esriFieldTypeString",
        "alias": "display",
        "length": 50
      },
      {
        "name": "OBJECTID",
        "type": "esriFieldTypeOID",
        "alias": "OBJECTID"
      }
    ],
    "features": [{
        "attributes": {
          "display": "Service Point",
          "OBJECTID": 13597
        },
        "geometry": {
          "x": -88.17599727899994,
          "y": 41.78465526100007
        }
      },
      {
        "attributes": {
          "display": "Service Point",
          "OBJECTID": 13598
        },
        "geometry": {
          "x": -88.17595382899998,
          "y": 41.78455803400004
        }
      },
      {
        "attributes": {
          "display": "Service Point",
          "OBJECTID": 13599
        },
        "geometry": {
          "x": -88.17582231499995,
          "y": 41.78435312600004
        }
      },
      {
        "attributes": {
          "display": "Service Point",
          "OBJECTID": 13600
        },
        "geometry": {
          "x": -88.17561004899994,
          "y": 41.784005335000074
        }
      },
      {
        "attributes": {
          "display": "Service Point",
          "OBJECTID": 13601
        },
        "geometry": {
          "x": -88.17557576699994,
          "y": 41.78393182000008
        }
      },
      {
        "attributes": {
          "display": "Service Point",
          "OBJECTID": 13602
        },
        "geometry": {
          "x": -88.17535967199996,
          "y": 41.78352876900004
        }
      },
      {
        "attributes": {
          "display": "Service Point",
          "OBJECTID": 13603
        },
        "geometry": {
          "x": -88.17534426199995,
          "y": 41.78340020400003
        }
      },
      {
        "attributes": {
          "display": "Service Point",
          "OBJECTID": 13649
        },
        "geometry": {
          "x": -88.17450698899995,
          "y": 41.78350136200004
        }
      },
      {
        "attributes": {
          "display": "Service Point",
          "OBJECTID": 13650
        },
        "geometry": {
          "x": -88.17435162999999,
          "y": 41.783597986000075
        }
      }
    ]
  }
];


var points1 = data.map((obj) => obj.features.attributes.display);
console.log(points1);

1 个答案:

答案 0 :(得分:1)

display阵列上使用另一张地图。真的不清楚预期结果是什么,因为有些没有var points1 = data.map((obj) => obj.features.map(({attributes})=>attributes.display || "NONE")); console.log(points1);并且有很多重复



<script>
var data = [{
    "displayFieldName": "",
    "fieldAliases": {
      "OBJECTID": "OBJECTID"
    },
    "fields": [{
      "name": "OBJECTID",
      "type": "esriFieldTypeOID",
      "alias": "OBJECTID"
    }],
    "features": [{
        "attributes": {
          "OBJECTID": 649
        }
      },
      {
        "attributes": {
          "OBJECTID": 665
        }
      },
      {
        "attributes": {
          "OBJECTID": 762
        }
      }
    ]
  },
  {
    "displayFieldName": "",
    "fieldAliases": {
      "display": "display",
      "OBJECTID": "OBJECTID"
    },
    "geometryType": "esriGeometryPoint",
    "spatialReference": {
      "wkid": 4326,
      "latestWkid": 4326
    },
    "fields": [{
        "name": "display",
        "type": "esriFieldTypeString",
        "alias": "display",
        "length": 50
      },
      {
        "name": "OBJECTID",
        "type": "esriFieldTypeOID",
        "alias": "OBJECTID"
      }
    ],
    "features": [{
        "attributes": {
          "display": "Transformer",
          "OBJECTID": 1537
        },
        "geometry": {
          "x": -88.17602806699995,
          "y": 41.78431233100008
        }
      },
      {
        "attributes": {
          "display": "Transformer",
          "OBJECTID": 1591
        },
        "geometry": {
          "x": -88.17546081099994,
          "y": 41.783341919000065
        }
      }
    ]
  },
  {
    "displayFieldName": "",
    "fieldAliases": {
      "display": "display",
      "OBJECTID": "OBJECTID"
    },
    "geometryType": "esriGeometryPoint",
    "spatialReference": {
      "wkid": 4326,
      "latestWkid": 4326
    },
    "fields": [{
        "name": "display",
        "type": "esriFieldTypeString",
        "alias": "display",
        "length": 50
      },
      {
        "name": "OBJECTID",
        "type": "esriFieldTypeOID",
        "alias": "OBJECTID"
      }
    ],
    "features": [{
        "attributes": {
          "display": "Service Point",
          "OBJECTID": 13597
        },
        "geometry": {
          "x": -88.17599727899994,
          "y": 41.78465526100007
        }
      },
      {
        "attributes": {
          "display": "Service Point",
          "OBJECTID": 13598
        },
        "geometry": {
          "x": -88.17595382899998,
          "y": 41.78455803400004
        }
      },
      {
        "attributes": {
          "display": "Service Point",
          "OBJECTID": 13599
        },
        "geometry": {
          "x": -88.17582231499995,
          "y": 41.78435312600004
        }
      },
      {
        "attributes": {
          "display": "Service Point",
          "OBJECTID": 13600
        },
        "geometry": {
          "x": -88.17561004899994,
          "y": 41.784005335000074
        }
      },
      {
        "attributes": {
          "display": "Service Point",
          "OBJECTID": 13601
        },
        "geometry": {
          "x": -88.17557576699994,
          "y": 41.78393182000008
        }
      },
      {
        "attributes": {
          "display": "Service Point",
          "OBJECTID": 13602
        },
        "geometry": {
          "x": -88.17535967199996,
          "y": 41.78352876900004
        }
      },
      {
        "attributes": {
          "display": "Service Point",
          "OBJECTID": 13603
        },
        "geometry": {
          "x": -88.17534426199995,
          "y": 41.78340020400003
        }
      },
      {
        "attributes": {
          "display": "Service Point",
          "OBJECTID": 13649
        },
        "geometry": {
          "x": -88.17450698899995,
          "y": 41.78350136200004
        }
      },
      {
        "attributes": {
          "display": "Service Point",
          "OBJECTID": 13650
        },
        "geometry": {
          "x": -88.17435162999999,
          "y": 41.783597986000075
        }
      }
    ]
  }
];


</script>
&#13;
// Andranik Keshishyan, Quiz 2
#include <iostream>
#include <string>
#include <iomanip> //For setw and other formatting
#include <ctime> //For random number generation
using namespace std;

int main()
{
    bool repeat = true; //Repeats program if true
    srand(time(0)); //Random number generation
    double amount_thrown, dice1, dice2, sum1, sum2, sum3, sum4, avg1, avg2, avg3; //Variables
    char space = ' '; //Space for Formatting

    cout <<"This program will roll 2 dice and calculate their sum and average depending on the amount of throws\n";
    cout <<"The amount of throws cannot exceed 12\n";

    while (repeat){ //Will repeat program if true.

        cout <<"How many times would you like the dice thrown?: ";
        cin >> amount_thrown;

        cout << "\n";

        if ((amount_thrown < 1 || amount_thrown > 12)||(!(cin>>amount_thrown))){ //Checks to see if user input is valid.
            cout <<"This is an invalid input of dice throws.\n";
        }
        else //Continues program if valid.
        {
            cout <<"Throw" << setw(3) << space <<"Die 1" << setw(3) << space << "Die 2" << setw(3) << space << "Sum\n";

            for(int x=1; x<=amount_thrown; x++){ //Loops program until amount of throws equals to user input

                dice1 = rand() % 6 + 1;
                dice2 = rand() % 6 + 1;
                sum1 = dice1+dice2; //Calculates sum of dice1 + dice 2
                sum2 += dice1; //Calculates sum of all dice 1 throws
                sum3 += dice2; // Calcules sum of all dice 2 throws
                sum4 += sum1; // Calculates summ of the sum
                avg1 = sum2/2; // Calculates avg of sum of dice 1 throws
                avg2 = sum3/2; // Calculates avg of sum of dice 2 throws
                avg3 = sum4/2; // Calculates ave of sum of dice 1 + dice 2

                cout << setw(2) << x << setw(7) << dice1 << setw(8) << dice2 << setw(9) << sum1 << endl;

            }

            cout << "-------------------------------------\n";
            cout << "Sum" << setw(5) << space << sum2 << setw(6) << space << sum3 << setw(6) << space << sum4 << endl;
            cout << "Avg" << setw(5) << space << avg1 << setw(5) << space << avg2 << setw(5) << space << avg3 << endl;


        }

        cout << "\nWould you like to roll the dice again?[y=repeat / anything else=stop]: ";
        char answer;
        cin >> answer;
        cin.ignore(1000, '\n'); //Makes sure other inputs are ignored
        answer=tolower(answer);
        repeat = answer == 'y'; //Program will repeat if user inputs true char y. 

    }
    cout << "Thank you, goodbye" << endl;

    return 0;
}
&#13;
&#13;
&#13;