英文:
Add to object with every iteration without replacing
问题
I have an object outside of the function that I want to populate with every iteration and call that object after the iteration is done but I am only able to see the last element in my object. How do I add to object instead of replacing?
我的代码:
let myobj = {};
const func1 = async (arg) => {
let table_id = result.data.internal_id;
const response = await axios_Func({ graphQL query });
console.log(`${response.data.name} - ${table_id}`);
myobj["name"] = response.data.name;
myobj["id"] = table_id;
await func2({ query: graphQL_query, table_id });
}
const func2 = async (result, table_id) => {
let dev = await getAllTables({ query: tables(123) });
let prod = await getAllTables({ query: tables(321) });
let prod_id = new Set();
result.data.data.table.table_fields.forEach((d) => {
if (d.connector != null && !(d.name in dev))
prod_id.add(prod[d.name]["id"]);
});
prod_id = [...prod_id];
prod_id.map((x) => createOne({ query: graphQL_query(x) });
return Promise.all(prod_id.map((x) => func1({ query: graphQL_query(x) })));
});
const callback = () => console.log(myobj);
const execute = async () => {
await func1()
callback()
}
myobj
返回的是对象中的最后一个元素,而不是应该被填充的 5 个元素。如何让我的对象包含所有元素?
英文:
I have an object outside of the function that I want to populate with every iteration and call that object after the iteration is done but I am only able to see the last element in my object. How do I add to object instead of replacing?
My code:
let myobj = {};
const func1 = async (arg) => {
let table_id = result.data.internal_id;
const response = await axios_Func({ graphQL query });
console.log(`${response.data.name} - ${table_id}`);
myobj["name"] = response.data.name;
myobj["id"] = table_id;
await func2({ query: graphQL_query, table_id });
}
const func2 = async (result, table_id) => {
let dev = await getAllTables({ query: tables(123) });
let prod = await getAllTables({ query: tables(321) });
let prod_id = new Set();
result.data.data.table.table_fields.forEach((d) => {
if (d. connector != null && !(d.name in dev))
prod_id.add(prod[d.name]["id"]);
});
prod_id = [...prod_id];
prod_id.map((x) => createOne({ query: graphQL_query(x) });
return Promise.all(prod_id.map((x) => func1({ query: graphQL_query(x) }));
});
const callback = () => console.log(myobj);
const execute = async () => {
await func1()
callback()
}
myobj
is returning the last element in the object rather than the 5 elements it is supposed to have been populated with.
How do I get my object to have all the elements?
答案1
得分: 1
你需要一个数组来存储多个对象。
let myarray = [];
const func1 = async (arg) => {
let table_id = result.data.internal_id;
const response = await axios_Func({ graphQL query });
console.log(`${response.data.name} - ${table_id}`);
const myobj = {};
myobj["name"] = response.data.name;
myobj["id"] = table_id;
myarray.push(myobj);
await func2({ query: graphQL_query, table_id });
}
英文:
You need an array to hold multiple objects.
let myarray = [];
const func1 = async (arg) => {
let table_id = result.data.internal_id;
const response = await axios_Func({ graphQL query });
console.log(`${response.data.name} - ${table_id}`);
const myobj = {};
myobj["name"] = response.data.name;
myobj["id"] = table_id;
myarray.push(myobj);
await func2({ query: graphQL_query, table_id });
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论