javascript - Combine associative array/object in JS without erasing numerically indexed values $.extend() -
when using $.extend(true, {}, obj1, obj2)
, resulting object erases numerically-indexed data number of results in obj2
.
a sample call $.extend()
this:
then take sample data instance:
var base_json_obj = { 'regions': [ 'nc', '-greensboro', 'va', '-richmond' ], 'vehicles': [ 'ford:escape', 'nissan:altima' ], 'turnover': { 'min': '0d', 'max': '5d' } }; var new_json_obj = { 'regions': [ 'fl', '-miami' ], 'vehicles': [ 'hyundai:sonata' ], 'turnover': { 'min': '1d', 'max': '6d' } }; var resulting_object = $.extend(true, {}, base_json_obj, new_json_obj);
the resulting object
{ 'regions': [ 'fl', '-miami', 'va', '-richmond' ], 'vehicles': [ 'hyundai:sonata', 'nissan:altima' ] 'turnover': { 'min': '1d', 'max': '6d' }, }
and here expected output. notice regions
has 6 values , vehicles
has 3 values.
{ 'regions': [ 'nc', '-greensboro', 'va', '-richmond', 'fl', '-miami' ], 'vehicles': [ 'ford:escape', 'nissan:altima', 'hyundai:sonata' ] 'turnover': { 'min': '1d', 'max': '6d' }, }
is there way modify call $.extend()
or use $.merge()
in way achieve this?
$.extend(true, ...)
treat arrays object, using value
, index
, 'min'
, key of 0
replaced.
you'll need write handles case. $.extend()
function relatively complicated, place start:
wiki
Comments
Post a Comment