JavaScript需要将2个单独的对象放在一起-但推送无效

问题描述 投票:-3回答:2

我需要添加新记录,但是无论push还是炸毁,我都会做任何事情>

不确定这是数组还是对象,但我猜它不是数组,因为推送不起作用

var first = {txtMedication: "ADVIL CAP  -  CAP 300MG", doseinput: "55"}

var second = {txtMedication: "Tylenol CAP  -  CAP 200MG", doseinput: "77"}

然后我想合并

first.push(second);   // push is not a function 

我的目标是最终得到这种类型的格式(我存储到本地存储中)。我试图创建一个新数组并将其推入其中,但最终得到了很多嵌套对象-我希望将其像新记录一样连接在一起,在同一级别,因此所有逗号在同一级别分开

更新:好的,因此,根据一个答案,我再次尝试了该数组,对每个数组进行了一次推送,但是数据被嵌套了,请记住,我不断提交FORM,并读取formdata,以及从localstorage中读取数据

这是几次提交点击后的数据

[{"xtxtMedication":"ADVIL        CAP  - ADVIL        CAP 200MG","doseinput":"44"},[{"xtxtMedication":"ADVIL        CAP  - ADVIL        CAP 200MG","doseinput":"44"},[{"xtxtMedication":"ADVIL        CAP  - ADVIL        CAP 200MG","doseinput":"44"},[{"xtxtMedication":"ADVIL        CAP  - ADVIL        CAP 200MG","doseinput":"44"},[{"xtxtMedication":"ADVIL        CAP  - ADVIL        CAP 200MG","doseinput":"44"},{"xtxtMedication":"ADVIL        CAP  - ADVIL        CAP 200MG","doseinput":"44"}]]]]]

更新2

这是一个显示推送问题的jsfiddle,我不想要2个数组,我想要将newData添加到第一个的单个数组

https://jsfiddle.net/jna79gs5/

我需要添加新记录,但是无论是否执行push或其他操作,我都会做任何事情不确定这是数组还是对象,但是我猜它不是数组,因为push不起作用var first = {...

javascript arrays json reactjs
2个回答
2
投票

firstobject。仅array包含推入。因此,如果需要带有firstsecond对象的数组,则必须执行以下操作:


1
投票
{txtMedication: "ADVIL CAP  -  CAP 300MG", doseinput: "55"}
© www.soinside.com 2019 - 2024. All rights reserved.