javascript - 将对象插入数组

标签 javascript arrays object express

所有,我正在尝试将对象插入数组并控制台。记录新数组以查看它的外观,但它似乎无法正常工作。你能告诉我我做错了什么吗?

<% var testObjArray = [] %>    
<% var testObj = {} %>

<% currentUser.shares.forEach(function(share){ %>
    <% if(!share || share.length < 1){ %>
    <!--DO NOTHING-->
    <% } else { %>
        <% if(currentUser.id == share.yes_owner.id){ %>
            <% testObj.price = share.event.yes_purchase_price %>
            <% testObj.yes_or_no = "YES" %>

        <% } else { %>

            <% testObj.price = share.event.no_purchase_price %>
            <% testObj.yes_or_no = "No" %>
        <% }%>
            <% testObj.quantity = 1 %>
            <% testObj.eventId = share.event.id %>
            <% testObj.name = share.event.name %>

        <% testObjArray.push(testObj) %>
        <% console.log(testObj) %>
    <% }%>
<% }) %>
<% console.log("THIS IS THE NEW OBJARRAY: " + testObjArray) %>

我希望最终结果是并且期待它是..

[{ price: undefined,
  yes_or_no: 'No',
  quantity: 1,
  eventId: 588a107edf666e0273614cca,
  name: 'Will this work?' },
{...}, 
{...},
{ price: undefined,
  yes_or_no: 'No',
  quantity: 1,
  eventId: 588a107edf666e0273614cca,
  name: 'Will this work?' }
...]

然而,结果就像......

“这是新的对象:” [object Object],[object Object],[object Object],[object Object]

最佳答案

改变这个

console.log("THIS IS THE NEW OBJARRAY: " + testObjArray)

console.log("THIS IS THE NEW OBJARRAY: ", testObjArray)

因为"string"+ Object使对象打印为字符串

关于javascript - 将对象插入数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41884883/

相关文章:

javascript - 导航栏/菜单在较小的屏幕上消失 - CSS 问题

javascript - 使用 onclick 从输入中获取值

python - 从两点之间的 numpy 数组获取值

flash - 如何设置 Flash 对象在新窗口中打开 URL?

javascript - 在 $rootScope 中存储用户数据——好还是坏?

javascript - 如何组合(遍历)2个字符串,混合每个字符串中不同长度的字母?

javascript - 如何修复我的代码并获取数组中第一个和最后一个数字的相反顺序

C 编程,char 指针数组

javascript - 显示给定输入数字的数据集中所有可能的字母组合

c - C中的面向对象