简体   繁体   English

如何使用 array.push 为数组添加值?

[英]How to use array.push to add value to an array?

I would like to add a value with array.push to my first element of array [0] to the field yesterday, I don't know very well what the structure is to be able to add this value.我想用array.push 将一个值添加到昨天我的数组[0] 的第一个元素到字段中,我不太清楚能够添加这个值的结构是什么。 try the following way试试下面的方法

var cumpleaños = [
  {
    ayer: "",
  },
  {
    hoy: "12-07-20",
}  ,
  {
    mañana: "12-08-20"
}
];
cumpleaños.push([0].ayer.("12-06-20"))
console.log(cumpleaños[0].ayer)

Thank you very much for the help!非常感谢你的帮助!

You don't need to push anything since you're not adding a new value, you're just modifying an existing value:您不需要推送任何内容,因为您没有添加新值,您只是在修改现有值:

cumpleaños[0].ayer = "12-06-20";

As a side note, your data structure would be much more effectively represented by a single object not inside an array:作为旁注,您的数据结构将更有效地由不在数组内的单个 object 表示:

const cumpleaños = {
    ayer: "12-06-20",
    hoy: "12-07-20",
    mañana: "12-08-20"
};

You would do assignment to the first element of the array.您将对数组的第一个元素进行赋值。 (If the oder is always the same) (如果顺序始终相同)

cumpleaños[0].ayer = "12-06-20"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM