1

I have two different options in JavaScript

Option 1:

        var _option = {
            "displayLength": _length,
            "order": [[ _order, ""+_sort+"" ]],
            "paging": _pages,
            "info": _info,
            "searching": _search
        }

Option 2:

        var _button_option = {
            dom: 'Bfrtip',
            buttons: [
                'copy',
                'excel',
                'print'
            ]
        }

I want to merge two option values to the one values

Result like this:

        var _option = {
            "displayLength": _length,
            "order": [[ _order, ""+_sort+"" ]],
            "paging": _pages,
            "info": _info,
            "searching": _search,
            dom: 'Bfrtip',
            buttons: [
                'copy',
                'excel',
                'print'
            ]
        }

How to merge two options in JavaScript?

Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
Jerry Kang
  • 13
  • 3
  • You can use spread syntax to combine two javascripts objects. var combined = {..._option, ..._button_option}; Read more about spread syntax on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax – jkpark Feb 18 '22 at 14:10
  • https://api.jquery.com/jquery.extend/ – freedomn-m Feb 18 '22 at 14:11

1 Answers1

2

You can use the Object.assign here

_option =  Object.assign({}, _option, _button_option)

or

Object.assign(_option, _button_option)

Or you can spread it like

_option = {..._option, ..._button_option}

For reference visit this

Obed Amoasi
  • 1,837
  • 19
  • 27
  • For anyone wondering why [tag:jquery] has a `$.extend` when this exists, please refer to: https://caniuse.com/mdn-javascript_builtins_object_assign – freedomn-m Feb 18 '22 at 14:17