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
3 Answers
Reset to default 6your 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
评论列表(0条)