簡體   English   中英

當我按下 React 中的按鈕時,如何在列表中添加新行?

[英]How can I add a new row to the list when I press the button in React?

我有一個輸入按鈕,當按下此按鈕時,它會在表格列表中添加一個新行。 我從 API 得到這樣一個數組。 只有這個數組的前 8 個對我有好處。 我用 slice 方法得到了前 8 個。 我已經閱讀了一些關於此的帖子,但無法理解。 我怎樣才能做到這一點? 在此處輸入圖像描述

你可以試試這個我不知道它是否回答了你的問題

let arr = [], // contains the 40 entries
    newArray = [];

arr.map((item) => {
  if(newArray.length <= 7){
     return newArray.push(item);
  }
}); 
console.log(newArray) // array with 8 entries
    

並添加一個新行

const createRow = (dt, main, weather, clouds, wind) => {
   return newArray.push({dt, main, weather, clouds, wind});
} 

使用 args 調用 createRow

createRow("20/02/2021 18:51",{...}, "cloudy","120 knots")

你只想要數組的前八個元素?

const arraysample = [1, 2, 3, 4, 5, 6, 7, 8, 9];

const firstEight = (array) => {
  let eight = [];
  arraysample.forEach((obj, i) => {
    if (i < 8) {
      eight.push(obj);
    }
  })
  return eight;
};

console.log(firstEight(arraysample))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM