Javascript 如何遍历Typescript中包含数组和对象的大型数组列表
我有一个数组。每个索引都有一个至少有10个属性的对象数组,我想映射所有对象,并以相同的顺序只存储这些对象的ID。我已经为此编写了这段代码 这是我的数据结构Javascript 如何遍历Typescript中包含数组和对象的大型数组列表,javascript,node.js,typescript,Javascript,Node.js,Typescript,我有一个数组。每个索引都有一个至少有10个属性的对象数组,我想映射所有对象,并以相同的顺序只存储这些对象的ID。我已经为此编写了这段代码 这是我的数据结构 organisationCompanyTalents = [ [ { id: '', fname: '', lname: '', other1: '', }, { id: '', fname: '',
organisationCompanyTalents = [
[
{
id: '',
fname: '',
lname: '',
other1: '',
},
{
id: '',
fname: '',
lname: '',
other1: '',
},
],
[
{
id: '',
fname: '',
lname: '',
other1: '',
},
],
];
let companyUserIds = Array();
organisationCompanyTalents.map(orgCmpTalent => {
let list = Array();
orgCmpTalent.map(companyTalent => {
list.push(companyTalent.company_user_id);
}),
companyUserIds.push(list);
});
现在,这段代码的问题是它没有以相同的顺序存储ID,总是在某个时候遍历较大的数组并首先存储,而在某个时候存储较小的数组。我如何才能使其有效,使其以相同的顺序返回
数据获取部分:
let organisationCompanyTalents = Array();
var orgTalentReview = Array();
if (companyOrganisations.length > 0) {
await Promise.all(
companyOrganisations.map(async org => {
await this.ComapanyTalentService.getCompanyTalentByOrganisationId(
org._id,
companyUser.Company_Id,
)
.then(companyTalent => {
if (companyTalent) {
organisationCompanyTalents.push(Object.values(companyTalent));
}
})
.catch(error => {
throw error;
});
}),
);
试试这个
让companyUserID=[];
OrganizationCompanyTalents.map(OrgCmpAlent=>{
for(设i=0;i
每个循环使用两个级别,如下所示:
const OrganizationCompanyTalents=[
[
{
id:6,
fname:'fname6',
lname:'lname6',
other1:‘other16’,
},
{
id:4,
fname:'fname4',
lname:'lname4',
其他1:‘其他14’,
},
],
[
{
id:9,
fname:'fname9',
lname:'lname9',
other1:‘other19’,
},
],
];
让companyUserId:any[]=[];
OrganizationCompanyTalents.forEach(人才=>{
let id:any[]=[];
人才。forEach(人才=>{
id.push(talent.id);
});
公司序列号推送(ids);
});
console.clear();
console.log(companyUserID);
此代码不起作用。我在ech索引上只给我一个ID我需要数组[ID]你的代码给我一个ID数组,但我需要数组[ID]每个索引都有相同顺序的ID列表,上面显示的数据请参见最新编辑答案是的,我现在尝试了,它给了我数组[ID]的数组,但同样的问题,它只是改变了我代码的顺序。我输出了答案,它总是[[6,4],[9]],我没有看到顺序的变化?您提到的顺序变化可能不是由代码引起的,但是当您进行页面刷新时对MongoDB的查询,所以是源数据顺序的变化,请确保在进行页面刷新时源数据的顺序是相同的。对于字符串对象ID,我想这是MongoDB的唯一用途。