使用地图功能填充数组问题



我不明白如何用一个map()填充数组,有一个区别: 我有一个数组,里面填充了一个模板,例如:

const templateOfRanks = {
rankingElementsTemplate : [
{row : [1.1, 1.2, 1.3, 1.4]},
{row : [2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9]},
{row : [3.1, 3.2, 3.3, 3.4]},
]
}

结果 它看起来像一个维度数组。如何在没有"行"的情况下填充新数组?我想从这个模板中获取一个包含元素但没有嵌套元素的数组。 它应该看起来像:[ 1.1, 1.2, 1.3, 1.4, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9,3.1, 3.2, 3.3, 3.4]

可能我应该使用push()功能,我不知道...但我需要有两个数组:第一个是维度的,第二个是单个数组。 请帮帮我。

你可以使用flatMap

const templateOfRanks = {
rankingElementsTemplate : [
{row : [1.1, 1.2, 1.3, 1.4]},
{row : [2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9]},
{row : [3.1, 3.2, 3.3, 3.4]},
]
}
const flat = templateOfRanks.rankingElementsTemplate.flatMap(i => i.row);
console.log(flat);

最新更新