data.map is not a function

后端 未结 9 1037
轮回少年
轮回少年 2020-12-02 05:50

I\'m bashing my head against an error I can\'t work out how to fix. I have the following;

JSON

{\"products\":
[
    {
        \"product_id\" : \"123         


        
相关标签:
9条回答
  • 2020-12-02 06:31

    If you want to map an object you can use Lodash. Just make sure it's installed via NPM or Yarn and import it.

    With Lodash:

    Lodash provides a function _.mapValues to map the values and preserve the keys.

    _.mapValues({ one: 1, two: 2, three: 3 }, function (v) { return v * 3; });
    
    // => { one: 3, two: 6, three: 9 }
    
    0 讨论(0)
  • 2020-12-02 06:32

    You can always do the following:

    const SomeCall = request.get(res => { 
    
    const Store = []; 
    Store.push(res.data);
    
    Store.forEach(item => { DoSomethingNeat 
    });
    }); 
    
    0 讨论(0)
  • 2020-12-02 06:35

    Objects, {} in JavaScript does not have the method .map(). It's only for Arrays, [].

    So in order for your code to work change data.map() to data.products.map() since products is an array which you can iterate upon.

    0 讨论(0)
提交回复
热议问题