javascript - Concat two arrays But concat is not working -
i have 2 arrays.i want concat both array values.but here shows me error,
newsarr.contact not function
my code
var globalnews = []; var commentarray = []; var commentarr = {}; commentarr['comment'] = comment; commentarray.push(commentarr); var newsarr = {}; newsarr['newsno'] = $("#newsnumber").val(); newsarr['desc'] = $("#desc").val(); //globalnews.push(newsarr.concat(commentarray)); // not working globalnews= newsarr.concat(commentarray);
you can use $.merge( commentarray, newsarray );
concatenate arrays:
$(document).ready(function() { var globalnews = []; var commentarray = []; var newsarray = []; var commentarr = {}; commentarr['comment'] = 'aaa'; commentarray.push(commentarr); var newsarr = {}; newsarr['newsno'] = 'bbb' newsarr['desc'] = 'vvv'; newsarray.push(newsarr); globalnews = $.merge( commentarray, newsarray ); console.log(globalnews) });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
Comments
Post a Comment