简体   繁体   中英

.map is not a function - TypeScript error

I am trying to get an array of City Names. But even though I am passing an array the browser still says.map is not a function. Here is the code I am trying to.

I have tried doing the below code, but still getting the similar error.

    const results = [
{
  "Name":"Dale (e)",
  "City":"Dale (e)",
  "StateAbbreviation":"IN",
  "StateName":"Indiana",
  "StateCode":26,
  "ZipCode":"47523"},
{
  "Name":"Dallas",
  "City":"Dallas",
  "StateAbbreviation":"TX",
  "StateName":"Texas",
  "StateCode":68,
  "ZipCode":"75202",
  }
 ];

    result = results.map(function (obj) {
      return obj.Name;
    });
    console.log(result);

I am trying to get an array for a AutoComplete text field for City Names.

Below is my response for result.

Array(5) [ "Austin", "Austin", "Austin Cart Hq", "Austin Metro Lakeline (e)", "Austin S Park Meadow (e)" ] App.js:76

Unhandled Rejection (TypeError): results.map is not a function

But browser is erroring out saying results.map is not a function.

My expected results should be in an array so that I can set the array to the state and it will display the autocomplete text field for the text box.

try this import { map } from 'rxjs/operators'; in the component and you have to use pipe before you can use map for example...

import { from } from 'rxjs';
import { map } from 'rxjs/operators';

//emit ({name: 'Joe', age: 30}, {name: 'Frank', age: 20},{name: 'Ryan', age: 50})
const source = from(results);
//grab each persons name, could also use pluck for this scenario
const example = source.pipe(map(({ Name }) => Name));
//output: "Joe","Frank","Ryan"
const subscribe = example.subscribe(val => console.log(val));

i hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM