gpt4 book ai didi

javascript - 将对象插入数组

转载 作者:行者123 更新时间:2023-11-29 16:45:40 25 4
gpt4 key购买 nike

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

<% 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com