我有一个表示数字钱包层次结构的JSON对象:
masterWallets = {wallet: {}, childs: [{}, {}, ..., {}]}
子项数组是其他钱包,可以包含其他钱包......(就像文件系统一样)。
这是我的html表:
<% masterWallets.forEach(function(masterWallet) { %>
<tr>
<td>masterWallet.wallet.walletName</td>
<td><%= JSON.stringify(masterWallet.childs) %></td>
<td>
<script>
document.write(window.getChilds( <%= masterWallet.childs %> ));
</script>
</td>
</tr>
<% }); %>
我想将子钱包(masterWallet.childs)数组传递给函数并提取并打印一些信息。我可以看到所有的孩子: &lt;%= JSON.stringify(mWallet.childs)%&gt; 但我无法将此变量传递给我的函数。我在javascript控制台中:
document.write(window.getChilds( [object Object],[object Object] ));
出现此错误:
Uncaught SyntaxError: Unexpected identifier
问题:如何将JSON对象(或JSON对象数组?)传递给函数?
答案 0 :(得分:0)
If your masterWallets is an object of wallet and childs then;
masterWallets = {wallet: {}, childs: [{wallet:{},childs[{}...{n}]}, ..., {n}]}
<% masterWallets.childs.forEach(function(wallet) { %>
<tr>
<td>masterWallets.wallet.walletName</td>
<td><%= JSON.stringify(wallet.childs) %></td>
<td>
<script>
document.write(window.getChilds( <%= wallet.childs %> ));
</script>
</td>
</tr>
<% }); %>
But if your masterWallets is a collection of wallets then;
masterWallets = {{wallet: {}, childs: [{}, {}, ..., {n}]},{wallet: {}, childs: [{}, {}, ..., {n}]}};
<% masterWallets.forEach(function(walletObj) { %>
<tr>
<td>walletObj.walet.walletName</td>
<td><%= JSON.stringify(walletObj.childs) %></td>
<td>
<script>
document.write(window.getChilds( <%= walletObj.childs %> ));
</script>
</td>
</tr>
<% }); %>