映射/挖掘嵌套在对象中的数组 - JavaScript

Posted

技术标签:

【中文标题】映射/挖掘嵌套在对象中的数组 - JavaScript【英文标题】:Mapping/digging into an array that is nested in an object - JavaScript 【发布时间】:2021-10-20 18:50:33 【问题描述】:

这是我关于堆栈溢出的第一个问题。我正在尝试映射一个位于对象内的数组。我的问题更多是关于定位而不是实际的映射过程本身(我认为)。我的代码的目标是将一个数组映射到这个目标:

var target = 
                        "id": 1, //as an int
                        "type": "donut",
                        "name": "Cake",
                        "ppu": 0.55,
                        "batters": "all of the batter types as a string",
                        "ingredients": [],//a copy of all the toppings
                        "countOfFillings": 0 
    

有问题的对象是:

var bakery = 
    "items":
        
            "item":
                [
                    
                        "id": "0001",
                        "type": "donut",
                        "name": "Cake",
                        "ppu": 0.55,
                        "batters":
                            
                                "batter":
                                    [
                                         "id": "1001", "type": "Regular" ,
                                         "id": "1002", "type": "Chocolate" ,
                                         "id": "1003", "type": "Blueberry" ,
                                         "id": "1004", "type": "Devil's Food" 
                                    ]
                            ,
                        "topping":
                            [
                                 "id": "5001", "type": "None" ,
                                 "id": "5002", "type": "Glazed" ,
                                 "id": "5005", "type": "Sugar" ,
                                 "id": "5007", "type": "Powdered Sugar" ,
                                 "id": "5006", "type": "Chocolate with Sprinkles" ,
                                 "id": "5003", "type": "Chocolate" ,
                                 "id": "5004", "type": "Maple" 
                            ]
                    ,
                    
                        "id": "0002",
                        "type": "donut",
                        "name": "Raised",
                        "ppu": 0.55,
                        "batters":
                            
                                "batter":
                                    [
                                         "id": "1001", "type": "Regular" 
                                    ]
                            ,
                        "topping":
                            [
                                 "id": "5001", "type": "None" ,
                                 "id": "5002", "type": "Glazed" ,
                                 "id": "5005", "type": "Sugar" ,
                                 "id": "5003", "type": "Chocolate" ,
                                 "id": "5004", "type": "Maple" 
                            ]
                    ,
        
                    
                        "id": "0003",
                        "type": "donut",
                        "name": "Old Fashioned",
                        "ppu": 0.55,
                        "batters":
                            
                                "batter":
                                    [
                                         "id": "1001", "type": "Regular" ,
                                         "id": "1002", "type": "Chocolate" 
                                    ]
                            ,
                        "topping":
                            [
                                 "id": "5001", "type": "None" ,
                                 "id": "5002", "type": "Glazed" ,
                                 "id": "5003", "type": "Chocolate" ,
                                 "id": "5004", "type": "Maple" 
                            ]
                    ,
                    
                        "id": "0004",
                        "type": "bar",
                        "name": "Bar",
                        "ppu": 0.75,
                        "batters":
                            
                                "batter":
                                    [
                                         "id": "1001", "type": "Regular" ,
                                    ]
                            ,
                        "topping":
                            [
                                 "id": "5003", "type": "Chocolate" ,
                                 "id": "5004", "type": "Maple" 
                            ],
                        "fillings":
                            
                                "filling":
                                    [
                                         "id": "7001", "name": "None", "addcost": 0 ,
                                         "id": "7002", "name": "Custard", "addcost": 0.25 ,
                                         "id": "7003", "name": "Whipped Cream", "addcost": 0.25 
                                    ]
                            
                    ,

                    
                        "id": "0005",
                        "type": "twist",
                        "name": "Twist",
                        "ppu": 0.65,
                        "batters":
                            
                                "batter":
                                    [
                                         "id": "1001", "type": "Regular" ,
                                    ]
                            ,
                        "topping":
                            [
                                 "id": "5002", "type": "Glazed" ,
                                 "id": "5005", "type": "Sugar" ,
                            ]
                    ,

                    
                        "id": "0006",
                        "type": "filled",
                        "name": "Filled",
                        "ppu": 0.75,
                        "batters":
                            
                                "batter":
                                    [
                                         "id": "1001", "type": "Regular" ,
                                    ]
                            ,
                        "topping":
                            [
                                 "id": "5002", "type": "Glazed" ,
                                 "id": "5007", "type": "Powdered Sugar" ,
                                 "id": "5003", "type": "Chocolate" ,
                                 "id": "5004", "type": "Maple" 
                            ],
                        "fillings":
                            
                                "filling":
                                    [
                                         "id": "7002", "name": "Custard", "addcost": 0 ,
                                         "id": "7003", "name": "Whipped Cream", "addcost": 0 ,
                                         "id": "7004", "name": "Strawberry Jelly", "addcost": 0 ,
                                         "id": "7005", "name": "Rasberry Jelly", "addcost": 0 
                                    ]
                            
                    
                ]
        
这是我的映射尝试。我能够生成数组,但只能从数组中的第一项生成,如 [0] 所示。任何反馈或建议将不胜感激!

    var exampleTest = bakery.items.item.map(mapCake);
    function mapCake (aCake)
    
        let newType = `$bakery.items.item[1].type` 
         
        let result = id: `$bakery.items.item[0].id`,
                    type: newType,
                    name: `$bakery.items.item[0].name`,
                    ppu: `$bakery.items.item[0].ppu`,
                    batters: [`$bakery.items.item[0].batters.batter`],
                    ingredients: `$bakery.items.item[0].topping`,
                    countOfFillings: `$bakery.items.item[0].fillings`.length;

        return  result

    

【问题讨论】:

【参考方案1】:

试试这个:

function mapCake(aCake) 
  let newType = `$aCake.type`;

  let result = 
    id: `$aCake.id`,
    type: newType,
    name: `$aCake.name`,
    ppu: `$aCake.ppu`,
    batters: [`$aCake.batters.batter`],
    ingredients: `$aCake.topping`,
    countOfFillings: `$aCake.fillings`.length
  ;

  return result;

看起来在你的函数中你每次都直接索引一个特定的面包店项目。

您的 aCake 参数尚未被使用,在每次迭代中,aCake 指的是正在迭代的当前项目 - 所以这就是您要填充结果值的内容。

【讨论】:

非常感谢您的帮助。这就说得通了。如果你不使用它,为什么要有一个参数。再次感谢您的宝贵时间!

以上是关于映射/挖掘嵌套在对象中的数组 - JavaScript的主要内容,如果未能解决你的问题,请参考以下文章

RestKit:将嵌套数组映射到对象

反应:映射嵌套的对象数组

Restkit 0.20 嵌套对象数组映射问题

带有数组映射的restkit嵌套对象

如何设置映射输入的嵌套 JSON 数组对象的状态

如何将 GraphQL 中的对象元素推送到 javascript 对象中的嵌套数组中?