Javascript 将多个对象转换为对象数组
我创建了一个函数,其中我将一个对象作为参数传递,并且有多个对象一个接一个地传递,输出是一个对象,但它作为一个单独的对象来,这很好,因为我以这种方式编写了代码,现在我想将对象存储到一个数组中,使其成为一个对象数组对于所有对象 下面是给出单个对象示例的代码Javascript 将多个对象转换为对象数组,javascript,arrays,node.js,json,google-cloud-firestore,Javascript,Arrays,Node.js,Json,Google Cloud Firestore,我创建了一个函数,其中我将一个对象作为参数传递,并且有多个对象一个接一个地传递,输出是一个对象,但它作为一个单独的对象来,这很好,因为我以这种方式编写了代码,现在我想将对象存储到一个数组中,使其成为一个对象数组对于所有对象 下面是给出单个对象示例的代码 const {db} = require('./constant') const momentTz = require("moment-timezone"); const { getAuth } = require("./constant");
const {db} = require('./constant')
const momentTz = require("moment-timezone");
const { getAuth } = require("./constant");
const officeStatus = async officeActivity => {
let output = {};
const maileventQuerySnapshot = await db
.collection("MailEvents")
.where("office", "==", officeActivity.office)
.where("event", "==", "delivered")
.limit(1).get();
output["office"] = officeActivity.office;
output["First Contact"] = officeActivity.attachment["First Contact"].value;
output["Second Contact"] = officeActivity.attachment["Second Contact"].value;
output["Office Creation Date"] = momentTz
.tz(officeActivity.timestamp, officeActivity.attachment.Timezone.value)
.format("DD MMM YYYY");
output["Reports Sent"] = maileventQuerySnapshot.docs.length ? true:false
const officeSubcollectionSnapshot = await db
.collection(`Offices/${officeActivity.officeId}/Addendum`)
.where("date","==",parseInt( momentTz(new Date()).subtract(1, "days").format("DD")))
.where('month', '==', parseInt( momentTz(new Date()).subtract(1, 'days').format("MM"))-1)
.where('year', '==',parseInt( momentTz(new Date()).subtract(1, "days").format("YYYY")))
.orderBy("user").orderBy('timestamp')
.get();
output['Number of unique Checkin Yesterday'] =
officeSubcollectionSnapshot.docs.length;
const activitiesRefSnapshot = await db
.collection("Activities")
.where("office", "==", officeActivity.office)
.where("template", "==", "subscription")
.where("attachment.Template.value", "==", "check-in")
.get();
const phoneNumberArray = [];
activitiesRefSnapshot.forEach(doc => {
phoneNumberArray.push(doc.data().attachment["Phone Number"].value);
});
const userRecord = await Promise.all(phoneNumberArray.map(getAuth));
output["Number of checkin Subscription Auth"] = userRecord.filter(
doc => doc !== undefined
).length;
output["Number of Checkin Subscription No Auth"] = userRecord.filter(
doc => doc === undefined
).length;
return { output};
};
module.exports = { officeStatus };
还有另一个文件,我在其中查询了office并将对象作为参数传递
const {admin,db} = require('./constant');
const { officeStatus } = require("./officeStatus");
let execute = async () => {
try {
let office = await db.collection("Offices").where("status", "==", "PENDING").get();
office.forEach(doc => {
officeStatus(doc.data())
.then(e => {
console.log(JSON.stringify(e.output));
})
.catch(error => {
console.log(error);
});
});
return;
} catch (error) {
console.log(error);
}
return;
};
execute();
admin.apps[0].delete();
I have get output in this way
{}
{}
{}....
and I wanted the output in this way
[{},{},{}]
尝试这样做:
// and the other file where i have queried the office and passed objects as an argument
const {admin,db} = require('./constant');
const { officeStatus } = require("./officeStatus");
let execute = async () => {
try {
let office = await db.collection("Offices").where("status", "==", "PENDING").get();
let arr = new Array();
office.forEach(doc => {
officeStatus(doc.data())
.then(e => {
arr.push(e)
})
.catch(error => {
console.log(error);
});
});
console.log(arr)
return;
} catch (error) {
console.log(error);
}
return;
};
execute();
admin.apps[0].delete();
我刚刚创建了一个数组,当我们使用officeStatus时,我们在其中推送
e
。然后,在代码末尾,我们记录arr
;声明的数组。forEach中的承诺不正确。它不会给你预期的结果。您不应该使用它,因为它不受支持。你应该考虑使用<>代码> 或<代码>承诺。所有< /代码>。在您的情况下,我建议使用Promise.all
作为数组的结果
Promise.all(office.docs.map(doc => {
return officeStatus(doc.data())
.then(e => {
return e.output;
})
.catch(error => {
console.log(error);
});
}))
.then(res => console.log(res));
当我使用它时,它会控制一个空数组,当我在内部控制时,你知道它会控制对象在你的内部,它会以我在示例中显示的相同方式打印,这取决于你在哪里控制。如果你把它放在officeStatus的.then中,你将把它作为单个对象,但是如果你把控制台放在.then中,它将把它们打印成数组。我复制了你的代码,映射没有按照你的解决方案工作。你得到了什么错误?
.then(res=>console.log(res))的输出是什么代码>它打印一个数组,但使用这种方式并不能解决我的问题,因为我必须将其转换为数组数组,并将其打印到excel文件中,所以您能否建议我一种方法,在其中定义数组并推送它们,这样就不存在范围问题
const {admin,db} = require('./constant');
const { officeStatus } = require("./officeStatus");
let execute = async () => {
let office = await db.collection("Offices").where("status", "==", "PENDING").get()
const arrayofObject = await Promise.all(office.docs.map(doc => {
return officeStatus(doc.data())
.then(e => {
// console.log(JSON.stringify(e.output));
return e.output;
})
.catch(error => {
console.log(error);
});
}))
console.log(arrayofObject)
return;
};
execute();
admin.apps[0].delete();