{"id":3490,"date":"2024-11-10T09:48:10","date_gmt":"2024-11-10T01:48:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3490\/"},"modified":"2024-11-10T09:48:10","modified_gmt":"2024-11-10T01:48:10","slug":"js%e5%a6%82%e4%bd%95%e5%90%88%e5%b9%b6json","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3490\/","title":{"rendered":"js\u5982\u4f55\u5408\u5e76json"},"content":{"rendered":"<blockquote><p>\n  \u5408\u5e76 json \u5bf9\u8c61\u53ef\u4ee5\u4f7f\u7528 object.assign() \u65b9\u6cd5\u6216\u6269\u5c55\u8fd0\u7b97\u7b26 (&#8230;)\u3002object.assign() \u65b9\u6cd5\u5c06\u591a\u4e2a\u5bf9\u8c61\u5408\u5e76\u5230\u4e00\u4e2a\u65b0\u5bf9\u8c61\u4e2d\uff0c\u800c\u6269\u5c55\u8fd0\u7b97\u7b26\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5bf9\u8c61\u5c55\u5f00\u5230\u53e6\u4e00\u4e2a\u5bf9\u8c61\u4e2d\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110813363862224.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u5408\u5e76json\u63d2\u56fe\" alt=\"js\u5982\u4f55\u5408\u5e76json\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 JavaScript \u5408\u5e76 JSON \u5bf9\u8c61<\/strong><\/p>\n<p><strong>\u5f00\u95e8\u89c1\u5c71\uff1a<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 Object.assign() \u65b9\u6cd5\u6216\u6269\u5c55\u8fd0\u7b97\u7b26 (&#8230;) \u6765\u5408\u5e76 JavaScript \u5bf9\u8c61\uff08\u5305\u62ec JSON \u5bf9\u8c61\uff09\u3002<\/p>\n<p><strong>\u8be6\u7ec6\u5c55\u5f00\uff1a<\/strong><\/p>\n<p><strong>\u65b9\u6cd5 1\uff1aObject.assign() \u65b9\u6cd5<\/strong><\/p>\n<p>Object.assign() \u65b9\u6cd5\u5141\u8bb8\u5c06\u591a\u4e2a\u5bf9\u8c61\u5408\u5e76\u5230\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\u4e2d\u3002\u5b83\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>Object.assign(target, ...sources);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>target \u662f\u76ee\u6807\u5bf9\u8c61\uff0c\u5408\u5e76\u540e\u7684\u7ed3\u679c\u5c06\u5b58\u50a8\u5728\u5176\u4e2d\u3002<\/li>\n<li>&#8230;sources \u662f\u8981\u5408\u5e76\u7684\u6e90\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre>const obj1 = { name: \"John\" };\nconst obj2 = { age: 30 };\nconst mergedObj = Object.assign({}, obj1, obj2);\n\nconsole.log(mergedObj); \/\/ { name: \"John\", age: 30 }<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u65b9\u6cd5 2\uff1a\u6269\u5c55\u8fd0\u7b97\u7b26 ( &#8230; )<\/strong><\/p>\n<p>\u6269\u5c55\u8fd0\u7b97\u7b26 (&#8230;) \u53ef\u4ee5\u5c06\u4e00\u4e2a\u5bf9\u8c61\u5c55\u5f00\u5230\u53e6\u4e00\u4e2a\u5bf9\u8c61\u4e2d\u3002\u5b83\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>const newObj = { ...obj1, ...obj2 };<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre>const obj1 = { name: \"John\" };\nconst obj2 = { age: 30 };\nconst mergedObj = { ...obj1, ...obj2 };\n\nconsole.log(mergedObj); \/\/ { name: \"John\", age: 30 }<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528\u6269\u5c55\u8fd0\u7b97\u7b26\u65f6\uff0c\u540e\u9762\u7684\u5bf9\u8c61\u5c06\u8986\u76d6\u524d\u9762\u5bf9\u8c61\u4e2d\u7684\u540c\u540d\u5c5e\u6027\u3002<\/li>\n<li>\u5982\u679c\u6e90\u5bf9\u8c61\u6709\u91cd\u590d\u7684\u5c5e\u6027\uff0c\u5219\u6700\u540e\u4e00\u4e2a\u6e90\u5bf9\u8c61\u4e2d\u7684\u5c5e\u6027\u503c\u5c06\u88ab\u4fdd\u7559\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u5408\u5e76json\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5408\u5e76 json \u5bf9\u8c61\u53ef\u4ee5\u4f7f\u7528 object.assign() \u65b9\u6cd5\u6216\u6269\u5c55\u8fd0\u7b97\u7b26 (&#8230;)\u3002object.assign() \u65b9\u6cd5\u5c06\u591a\u4e2a\u5bf9\u8c61\u5408\u5e76\u5230\u4e00\u4e2a\u65b0\u5bf9\u8c61\u4e2d\uff0c\u800c\u6269\u5c55\u8fd0\u7b97\u7b26\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5bf9\u8c61\u5c55\u5f00\u5230\u53e6\u4e00\u4e2a\u5bf9\u8c61\u4e2d\u3002 \u5982\u4f55\u4f7f\u7528 JavaScript \u5408\u5e76 JSON \u5bf9\u8c61 \u5f00\u95e8\u89c1\u5c71\uff1a \u53ef\u4ee5\u4f7f\u7528 Object.assign() \u65b9\u6cd5\u6216\u6269\u5c55\u8fd0\u7b97\u7b26 (&#8230;) \u6765\u5408\u5e76 JavaScript \u5bf9\u8c61\uff08\u5305\u62ec JSON \u5bf9\u8c61\uff09\u3002 \u8be6\u7ec6\u5c55\u5f00\uff1a \u65b9\u6cd5 1\uff1aObject.assign() \u65b9\u6cd5 Object.assign() \u65b9\u6cd5\u5141\u8bb8\u5c06\u591a\u4e2a\u5bf9\u8c61\u5408\u5e76\u5230\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\u4e2d\u3002\u5b83\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a Object.assign(target, &#8230;sources); \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a target \u662f\u76ee\u6807\u5bf9\u8c61\uff0c\u5408\u5e76\u540e\u7684\u7ed3\u679c\u5c06\u5b58\u50a8\u5728\u5176\u4e2d\u3002 &#8230;sources \u662f\u8981\u5408\u5e76\u7684\u6e90\u5bf9\u8c61\u3002 \u793a\u4f8b\uff1a const obj1 = { name: &#8220;John&#8221; }; const obj2 = { age: 30 }; const mergedObj = Object.assign({}, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3490","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3490","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3490"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3490\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3490"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3490"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3490"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}