亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

不同對象內矩陣的數學運算

不同對象內矩陣的數學運算

BIG陽 2023-08-10 10:51:45
在 javascript 中,我有以下對象:console.log(data)(10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]0: {species: "Citronela paniculata", cbh: Array(1)}1: {species: "Myrcia splendens", cbh: Array(1)}2: {species: "Araucaria angustifolia", plot: 1, cbh: Array(1)}3: {species: "Bacharis montana", cbh: Array(1)}4:cbh: (2) [10, 20]plot: 1species: "Casearia decandra"__proto__: Object5: {cbh: Array(1), species: "Bacharis montana"}6: {cbh: Array(3), species: "Ilex paraguariensis"}7: {cbh: Array(1), species: "Ilex paraguariensis"}8: {species: "Ilex paraguariensis", cbh: Array(1)}9: {plot: 1, cbh: Array(1), species: "Araucaria angustifolia"}length: 10__proto__: Array(0)我想將數組 cbh 的每個元素除以 pi,然后我使用:let newData = data.map(({ plot, species, cbh }) => {        let dbh = cbh/Math.PI;        return { plot, species, cbh, dbh };    })但對于那些具有多個元素的數組,我得到 NaN: console.log(newData)(10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]0: {plot: undefined, species: "Citronela paniculata", cbh: Array(1), dbh: 9.549296585513721}1: {plot: undefined, species: "Myrcia splendens", cbh: Array(1), dbh: 10.185916357881302}2: {plot: 1, species: "Araucaria angustifolia", cbh: Array(1), dbh: 5.729577951308232}3: {plot: undefined, species: "Bacharis montana", cbh: Array(1), dbh: 4.7746482927568605}4:cbh: (2) [10, 20]dbh: NaNplot: 1species: "Casearia decandra"__proto__: Object5: {plot: undefined, species: "Bacharis montana", cbh: Array(1), dbh: 6.366197723675814}6: {plot: undefined, species: "Ilex paraguariensis", cbh: Array(3), dbh: NaN}7: {plot: undefined, species: "Ilex paraguariensis", cbh: Array(1), dbh: 6.366197723675814}8: {plot: undefined, species: "Ilex paraguariensis", cbh: Array(1), dbh: 15.915494309189533}9: {plot: 1, species: "Araucaria angustifolia", cbh: Array(1), dbh: 15.915494309189533}length: 10__proto__: Array(0)如何將cbh中的每個元素除以pi?任何提示都會很棒!先感謝您!
查看完整描述

1 回答

?
慕容森

TA貢獻1853條經驗 獲得超18個贊

將只有一個元素的數組除以 時Math.PI,結果是一個數字。單元素數組會隱式轉換為數字,但元素較多的數組無法轉換為數字,因此得到 NaN。無論如何,這種除法都行不通,因為輸出不會返回數組,而是一個數字或 NaN。

要實現目標結果,您可以使用map 函數,它將源數組轉換為新數組,并對源數組的每個元素應用指定的轉換(在本例中為除以Math.PI):

let newData = data.map(({ plot, species, cbh }) => {

? ? const dbh = cbh.map(value => value / Math.PI);


? ? return { plot, species, cbh, dbh };

})


查看完整回答
反對 回復 2023-08-10
  • 1 回答
  • 0 關注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號