Javascript 不同对象内矩阵的数学运算

Javascript 不同对象内矩阵的数学运算,javascript,arrays,Javascript,Arrays,在javascript中,我有以下对象: console.log(data) (10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] 0: {species: "Citronela paniculata", cbh: Array(1)} 1: {species: "Myrcia splendens", cbh: Array(1)} 2: {species: "Araucaria angus

在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: 1
species: "Casearia decandra"
__proto__: Object
5: {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: NaN
plot: 1
species: "Casearia decandra"
__proto__: Object
5: {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?任何提示都会很棒!提前谢谢你

用一个元素的数组除以
Math.PI
,结果是一个数字。一个元素数组隐式地转换为一个数字,但是一个包含大量元素的数组不能转换为一个数字,因此获得NaN。在任何情况下,这种除法都不起作用,因为输出将不返回数组,而是返回数字或NaN

要实现目标结果,可以使用,它将源数组转换为新数组,并应用 对源数组的每个元素指定的转换(在本例中,除以
Math.PI
):


您必须遍历数组,并将每个元素分开。您可以使用阵列原型上的
.map()
函数来实现这一点。
let newData = data.map(({ plot, species, cbh }) => {
    const dbh = cbh.map(value => value / Math.PI);

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