0

I have existing JSON data, and I would like to build a similar JSON object using map functions, however the nested .map() methods are returning the data type and not the actual data for the output.

I have this starting data:

{
        "id": 111,
        "other_id": 111,
        "name": "name",
        "display_name": "display name",
        "type": "type",
        "required": true,
        "sort_order": 0,
        "config": {
            "config_one": true,
            "config_two": true,
            "config_three": "weight"
        },
        "option_values": [
            {
                "id": 2222,
                "other_option_id": 2222,
                "label": "Option Value Label",
                "sort_order": 0,
                "value_data": {
                    "separate_object_id": 3333
                },
                "is_default": false,
                "adjusters": {
                    "price": null,
                    "weight": null,
                    "image_url": "",
                    "enable_disable_flag": {
                        "status": false,
                        "message": ""
                    }
                }
            }
        ]
    }

And would like it to result in this data after map function:

{
    "display_name": "display name",
    "type": "type",
    "required": true,
    "sort_order": 0,
    "config": {
            "config_one": true,
            "config_two": true,
            "config_three": "weight"
    },
    "option_values": [
        {
            "label": "Option Value Label",
            "sort_order": 0,
            "value_data": {
                "separate_object_id": 3333
            },
            "is_default": false,
            "adjusters": {
                "price": null,
                "weight": null,
                "image_url": "",
                "enable_disable_flag": {
                    "status": false,
                    "message": ""
                }
            }
        }
    ]
}

This is my current map method function that I'm using, that is not working:

  modifier.map(item => {
      let returnedItem = {
        display_name: item.display_name,
        type: item.type,
        required: item.required,
        sort_order: item.sort_order,
        config: item.config,
        option_values: item.option_values.map(opt_valItems => ({ 
          label: opt_valItems.label, 
          sort_order: opt_valItems.sort_order,
          value_data: opt_valItems.value_data,
          is_default: opt_valItems.is_default,
          adjusters: item.adjusters
        }))
      }  
    })
3
  • 1
    your map doenst return anything Commented May 28, 2020 at 6:30
  • JSON is a textual notation for data exchange. (More here.) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON. Commented May 28, 2020 at 6:34
  • What is modifier in your code? It's clearly not the structure quoted at the beginning of the question, since that object doesn't have a map method. Is it an array of that structure? What are you doing with map`'s return value? Commented May 28, 2020 at 6:36

1 Answer 1

2

Map always returns a new Array. So you need to assign this into a new variable. Also, there was a slight problem in your code. You were using let returnedItem inside the map, but were not returning anything. Fixed that.

var modifier = [{
        "id": 111,
        "other_id": 111,
        "name": "name",
        "display_name": "display name",
        "type": "type",
        "required": true,
        "sort_order": 0,
        "config": {
            "config_one": true,
            "config_two": true,
            "config_three": "weight"
        },
        "option_values": [
            {
                "id": 2222,
                "other_option_id": 2222,
                "label": "Option Value Label",
                "sort_order": 0,
                "value_data": {
                    "separate_object_id": 3333
                },
                "is_default": false,
                "adjusters": {
                    "price": null,
                    "weight": null,
                    "image_url": "",
                    "enable_disable_flag": {
                        "status": false,
                        "message": ""
                    }
                }
            }
        ]
    }]


 var x = modifier.map(item => {
      return {
        display_name: item.display_name,
        type: item.type,
        required: item.required,
        sort_order: item.sort_order,
        config: item.config,
        option_values: item.option_values.map(opt_valItems => ({ 
          label: opt_valItems.label, 
          sort_order: opt_valItems.sort_order,
          value_data: opt_valItems.value_data,
          is_default: opt_valItems.is_default,
          adjusters: item.adjusters
        }))
      }  
    });
    
    console.log(x)

Sign up to request clarification or add additional context in comments.

4 Comments

"Also, fixed your code slightly" Please call out the changes you made, and why you made them.
@T.J.Crowder Thank you. Updated the answer.
Hey Thanks @T.J.Crowder But the option_values still is just returning [Object] :(
Nvm, this was console printing issue with console.log that made me believe the values weren't there. Thanks again. For others that MIGHT have this issue, I stringified the json, then printed it as such console.log(JSON.stringify(obj, null, 2)) This made it print all the nested items.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.