NodeJS 如何循环索引id的数组,并在公社,类别和位置中逐一循环?

d6kp6zgx  于 2022-12-29  发布在  Node.js
关注(0)|答案(1)|浏览(78)

这里我正在使用nodejs的typescript API。我正在使用map循环获取对象的数据数组。我正在使用issuerId从另一个API函数获取“公社”“类别”和“位置”数据。我没有获取“公社”“类别”和“位置”。我在下面附上了我的代码。

Object.values(premiumValue).map(async(x:any,index:any)=>{
    var issuerId = await Object.values(premiumValue)[index].issuer_id
   
  var Communes = await employerDetail.getSingleProfileType(issuerId, 'Communes')
 
     var category = await employerDetail.getSingleProfileType(issuerId, 'company_type')
     
     var location = await employerDetail.getSingleProfileType(issuerId, 'location')

     Object.assign(values[issuerId], { Communes }, { category }, { location })
     
  })
return Object.values(values)

我只得到这种数据

[
    {
        "issuer_id": 64,
        "company_name": "Gastro Südtirol",
        "Total_Job": 2
    },
    {
        "issuer_id": 70,
        "company_youtube": "https://www.youtube.com/channel/UCB2bOahchY6Hsc_WXnQ-NCw",
        "company_name": "Auto Hofer",
        "Total_Job": 2
    },
    {
        "issuer_id": 72,
        "company_name": "Assimeran GmbH",
        "Total_Job": 2
    }
]

我需要这种数据

[
    {
        "issuer_id": 64,
        "company_name": "Gastro Südtirol",
        "Total_Job": 2,
        "Communes": [],
        "category": [],
        "location": [
            {
                "id": 907,
                "location": "brixen"
            }
        ]
    },
    {
        "issuer_id": 70,
        "company_youtube": "https://www.youtube.com/channel/UCB2bOahchY6Hsc_WXnQ-NCw",
        "company_name": "Auto Hofer",
        "Total_Job": 2,
        "Communes": [],
        "category": [],
        "location": [
            {
                "id": 907,
                "location": "brixen"
            }
        ]
    },
    {
        "issuer_id": 72,
        "company_name": "Assimeran GmbH",
        "Total_Job": 2,
         "Communes": [],
        "category": [],
        "location": [
            {
                "id": 907,
                "location": "brixen"
            }
        ]
    }
 ]

但是我在没有communescategorylocation"的情况下获取数据。这里是否存在异步/等待问题?如何使用issuerId循环communescategorylocation数据?

iyr7buue

iyr7buue1#

在不知道API数据的形式和values是什么的情况下,下面让我们来猜测一下如何使用async/await拉入数据并像您尝试的那样格式化它。你可以使用传统的for循环或者for...of,并将所有内容 Package 在一个async函数中:

/***** IGNORE ALL OF THIS, JUST SETUP CODE ****/
const api = [
    {
        issuer_id: 64,
        Communes: [],
        company_type: [],
        location: [
            {
                id: 907,
                location: "brixen",
            },
        ],
    },
    {
        issuer_id: 70,
        Communes: [],
        company_type: [],
        location: [
            {
                id: 907,
                location: "brixen",
            },
        ],
    },
    {
        issuer_id: 72,
        Total_Job: 2,
        Communes: [],
        company_type: [],
        location: [
            {
                id: 907,
                location: "brixen",
            },
        ],
    },
];

const values = {
    64: {
        issuer_id: 64,
        company_name: "Gastro Südtirol",
        Total_Job: 2,
    },
    70: {
        issuer_id: 70,
        company_youtube: "https://www.youtube.com/channel/UCB2bOahchY6Hsc_WXnQ-NCw",
        company_name: "Auto Hofer",
        Total_Job: 2,
    },
    72: {
        issuer_id: 72,
        company_name: "Assimeran GmbH",
        Total_Job: 2,
    },
};

/*
 * Ignore this, it just makes the async stuff actually mimic an API call
 */
function sleep() {
    return new Promise((resolve, reject) => {
        const time = Math.random() * 500;
        setTimeout(() => resolve(time), time);
    });
}

const employerDetail = {
    getSingleProfileType: async function (issuerId, key) {
        await sleep();
        return new Promise((resolve, reject) => {
            const foundIndex = api.findIndex((el) => el.issuer_id == issuerId);
            if (foundIndex === -1) {
                reject(`No data in API for issuerId: ${issuerId}`);
            } else {
                resolve(api[foundIndex][key]);
            }
        });
    },
};

/**** THIS IS WHERE YOU WOULD START USING THINGS ***/

async function getAPIData() {
    const apiData = [];
    // I'm just looping through the IDs I see in your output
    for (const issuerId of [64, 70, 72]) {
        const Communes = await employerDetail.getSingleProfileType(issuerId, "Communes");
        const category = await employerDetail.getSingleProfileType(issuerId, "company_type");
        const location = await employerDetail.getSingleProfileType(issuerId, "location");
        apiData.push(Object.assign(values[issuerId], { Communes, category, location }));
    }
    return apiData;
}

async function main() {
    const apiData = await getAPIData();
    console.log(JSON.stringify(apiData, null, 2));
}

main();

相关问题