javascript - clone an object's element and push to it's self -
i have object a.
if json.stringify(a)
, result:
{ "ordera": [{ "orderid": "19", "serverid": 129, "description": "apple", "status": "1", "details": "" }] }
now, clone it's own element , make larger object this:
{ "ordera": [{ "orderid": "19", "serverid": 129, "description": "apple", "status": "1", "details": "" }], "ordera": [{ "orderid": "19", "serverid": 129, "description": "apple", "status": "1", "details": "" }], "ordera": [{ "orderid": "19", "serverid": 129, "description": "apple", "status": "1", "details": "" }] }
how achieve using lodash/underscore/ or jquery?
so far, i've tried jquery.extend, lodash union, , didn't work.
object keys have unique, otherwise overwrite held data. see demo.
here's how:
var = { "ordera": [{ "orderid": "19", "serverid": 129, "description": "apple", "status": "1", "details": "" }] }; ['b','c','d'].foreach(function(letter,index) { all['order'+letter] = $.extend(true, {}, all.ordera); }); console.log( json.stringify( ) );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Comments
Post a Comment