将每次迭代都添加到对象中,而不是替换。

huangapple go评论78阅读模式
英文:

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 });
}

huangapple
  • 本文由 发表于 2023年5月11日 00:44:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220836.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定