迭代时创建一个数组 Angular
Create an array while iterating Angular
当我 select 几行时,我从数组中获取 ID(大量数据传入,我只需要 ID)。我希望在获取 ID 时仅使用 ID 为我创建一个数组。问题是,当我尝试时,我得到以下信息(通过控制台):
编号选择:78
编号选择:79
编号选择:81
我想将它们作为普通数组获取:
{ 78, 79, 81 }
TS
procesarClic() {
const request = this.selection.selected;
for (let i = 0; i < request.length; i++){
const list = request[i].id;
console.log('Id Seleccionado: ', list);
}
}
请求常量它是 selected 行与数组中所有数据的位置,因为许多行可以 selected。
感谢您的贡献和帮助!
您必须创建数组并像这样填充它:
procesarClic() {
const request = this.selection.selected;
let array = [];
for (let i = 0; i < request.length; i++){
const list = request[i].id;
console.log('Id Seleccionado: ', list);
array.push(request[i].id);
}
}
这样,您将得到一个仅包含 ID 的数组。
BR
假设 this.selection.selected
是一个数组,你可以使用 map
函数,即
const examples = [
{id: 1, name: "One"},
{id: 2, name: "Two"},
{id: 3, name: "Three"}
]
const onlyIds = examples.map(e => e.id);
console.log(onlyIds);
这将 return 一个仅包含 ID 的数组。
当我 select 几行时,我从数组中获取 ID(大量数据传入,我只需要 ID)。我希望在获取 ID 时仅使用 ID 为我创建一个数组。问题是,当我尝试时,我得到以下信息(通过控制台):
编号选择:78
编号选择:79
编号选择:81
我想将它们作为普通数组获取:
{ 78, 79, 81 }
TS
procesarClic() {
const request = this.selection.selected;
for (let i = 0; i < request.length; i++){
const list = request[i].id;
console.log('Id Seleccionado: ', list);
}
}
请求常量它是 selected 行与数组中所有数据的位置,因为许多行可以 selected。
感谢您的贡献和帮助!
您必须创建数组并像这样填充它:
procesarClic() {
const request = this.selection.selected;
let array = [];
for (let i = 0; i < request.length; i++){
const list = request[i].id;
console.log('Id Seleccionado: ', list);
array.push(request[i].id);
}
}
这样,您将得到一个仅包含 ID 的数组。
BR
假设 this.selection.selected
是一个数组,你可以使用 map
函数,即
const examples = [
{id: 1, name: "One"},
{id: 2, name: "Two"},
{id: 3, name: "Three"}
]
const onlyIds = examples.map(e => e.id);
console.log(onlyIds);
这将 return 一个仅包含 ID 的数组。