javascript - Merging two json objects in Java script? - Stack Overflow

Have u merged two jsons in javascript ??Problem:a={id:123,name:john,status:success};b={id:123,status:i

Have u merged two jsons in javascript ??

Problem:

a={id:123,name:john,status:success};
b={id:123,status:inprocess,transId:245};

Output json should be like

{id:123,name:john,status:success,transId:245};

All the values from a should override the ones in b and also the unmon key/values should appear in output json.

I tried out some recursive options but cudnt acheive the output.

Have u merged two jsons in javascript ??

Problem:

a={id:123,name:john,status:success};
b={id:123,status:inprocess,transId:245};

Output json should be like

{id:123,name:john,status:success,transId:245};

All the values from a should override the ones in b and also the unmon key/values should appear in output json.

I tried out some recursive options but cudnt acheive the output.

Share Improve this question edited Feb 26, 2013 at 11:21 ѕтƒ 3,64710 gold badges50 silver badges81 bronze badges asked Feb 26, 2013 at 11:16 Krithika VittalKrithika Vittal 1,4672 gold badges17 silver badges33 bronze badges 3
  • 3 That's not JSON, that is Javascript objects. JSON is a test format for representing objects. – Guffa Commented Feb 26, 2013 at 11:20
  • It's been asked so many times: stackoverflow./questions/4154712/… stackoverflow./questions/10430279/… stackoverflow./questions/5585168/… – oleq Commented Feb 26, 2013 at 11:22
  • Hi oleq, i tried those links but cudnt achieve the needed result.I was careful to already search the stack before asking – Krithika Vittal Commented Feb 26, 2013 at 11:38
Add a ment  | 

3 Answers 3

Reset to default 6

your a and b variable are not valid json.

<script>
//change your a and b variable to this.
 a={id:123,name:'john',status:'success'}; 
 b={id:123,status:'inprocess',transId:245};
$(document).ready(function(){

  $.extend(a,b);

});
</script>

and a will have structure like

{
    id: 123
    name: "john"
    status: "inprocess"
    transId: 245
 
}

I've used jquery api

update.

without jquery

   a={id:123,name:'john',status:'success'}; 
   b={id:123,status:'inprocess',transId:245};

  extend(a,b);

where extend function is:

 function extend(a, b){
    for(var key in b)
        if(b.hasOwnProperty(key))
            a[key] = b[key];
    return a;
 }

ref1 ,ref2 , ref3

It's simple

for (var p in a)
   b[p] = a[p];
let x = {
  a: 1,
  b: 2,
  c: 3  
}

let y = {
  c: 4, 
  d: 5,
  e: 6
}

let z = Object.assign(x, y)

console.log(z)

z:
{
  a:1,
  b:2,
  c:4, 
  d:5,
  e:6,
}

---> NOTICE : the object z take the y's c attribute

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742287510a4415590.html

相关推荐

  • javascript - Merging two json objects in Java script? - Stack Overflow

    Have u merged two jsons in javascript ??Problem:a={id:123,name:john,status:success};b={id:123,status:i

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信