使用 javascript / node js 在 map 函数之外获取数据
fetching data oustide the map function using javascript / node js
let object = [
{
id: '01',
name: 'Subject',
'Data.type': 'maths',
},
{
id: '02',
name: 'Subject',
'Data.type': 'science',
},
{
id: '04',
name: 'language',
'Data.type': 'node',
},
{
id: '05',
name: 'language',
'Data.type': 'node',
}
/*...*/
];
let type=[];
let result=[];
object.map(async (value) => {
type.push(value["Data.type"]);
if(some condition){
// 1st condition
if(some condition){
// 2nd condition
if(some condition){
let path= await functionName();
// 3rd conditon
if(some condtion){
// 4th condition
result.push("task");
}
}
}
}
// I can fetch result till here console.log(result)
});
// i can't fetch result here and i can't put condtion here as data present inside dObject is dummy for refrence purpose only
console.log(type);
console.log(result);
我在 map 函数外声明了两个数组我可以轻松获取名称数组但无法获取结果数组我不知道为什么但它的范围在 map 函数内结束有没有其他方法我可以获取结果在地图功能之外
- 为了参考,我发布了我的名字功能,它工作正常
let object = [
{
id: '01',
name: 'Subject',
'Data.type': 'maths',
},
{
id: '02',
name: 'Subject',
'Data.type': 'science',
},
{
id: '04',
name: 'language',
'Data.type': 'node',
},
{
id: '05',
name: 'language',
'Data.type': 'node',
}
];
let type=[];
let result=[];
object.map(async (value) => {
type.push(value["Data.type"]);
});
// i can't fetch result here and i can't put condtion here as data present inside dObject is dummy for refrence purpose only
console.log(type);
console.log(result);
在这里我可以获取我已经完美声明的类型数组,但是在第一个片段中我无法获取地图函数之外的结果
- 因为我完美地写了我的条件,这就是为什么它 运行 完全在 map 函数内而不是在 map 函数外
好吧 .map()
不支持异步。它不会暂停循环以等待您的 await
因此您最终会在异步操作将任何内容推入数组之前尝试使用该数组。这是一个时间问题。
您可以在 .map()
returns 的结果数组上使用 await Promise.all()
或者您可以切换到 for
循环,即 async
知道并会为您的 await
.
暂停循环
仅供参考,如果您对结果数组不感兴趣,则永远不要使用 .map()
returns,因为这就是它的全部意义。有更有效的方法来迭代数组。而且,在做异步事情时,现在 await
的普通 for
循环要强大得多,因为您可以完全控制 .forEach()
和 .map()
的循环] 不提供,因为他们 async
不知道。
您没有显示太多实际代码,但这是 for
循环的一般思路:
async someFunction() {
let type=[];
let result=[];
for (let value of object) {
type.push(value["Data.type"]);
if(some condition){
// 1st condition
if(some condition){
// 2nd condition
if(some condition){
let path= await functionName();
// 3rd conditon
if(some condtion){
// 4th condition
result.push("task");
}
}
}
}
}
console.log(result);
}
someFunction().then(() => {
console.log("all done");
}).catch(err => {
console.log(err);
});
let object = [
{
id: '01',
name: 'Subject',
'Data.type': 'maths',
},
{
id: '02',
name: 'Subject',
'Data.type': 'science',
},
{
id: '04',
name: 'language',
'Data.type': 'node',
},
{
id: '05',
name: 'language',
'Data.type': 'node',
}
/*...*/
];
let type=[];
let result=[];
object.map(async (value) => {
type.push(value["Data.type"]);
if(some condition){
// 1st condition
if(some condition){
// 2nd condition
if(some condition){
let path= await functionName();
// 3rd conditon
if(some condtion){
// 4th condition
result.push("task");
}
}
}
}
// I can fetch result till here console.log(result)
});
// i can't fetch result here and i can't put condtion here as data present inside dObject is dummy for refrence purpose only
console.log(type);
console.log(result);
我在 map 函数外声明了两个数组我可以轻松获取名称数组但无法获取结果数组我不知道为什么但它的范围在 map 函数内结束有没有其他方法我可以获取结果在地图功能之外
- 为了参考,我发布了我的名字功能,它工作正常
let object = [
{
id: '01',
name: 'Subject',
'Data.type': 'maths',
},
{
id: '02',
name: 'Subject',
'Data.type': 'science',
},
{
id: '04',
name: 'language',
'Data.type': 'node',
},
{
id: '05',
name: 'language',
'Data.type': 'node',
}
];
let type=[];
let result=[];
object.map(async (value) => {
type.push(value["Data.type"]);
});
// i can't fetch result here and i can't put condtion here as data present inside dObject is dummy for refrence purpose only
console.log(type);
console.log(result);
在这里我可以获取我已经完美声明的类型数组,但是在第一个片段中我无法获取地图函数之外的结果
- 因为我完美地写了我的条件,这就是为什么它 运行 完全在 map 函数内而不是在 map 函数外
好吧 .map()
不支持异步。它不会暂停循环以等待您的 await
因此您最终会在异步操作将任何内容推入数组之前尝试使用该数组。这是一个时间问题。
您可以在 .map()
returns 的结果数组上使用 await Promise.all()
或者您可以切换到 for
循环,即 async
知道并会为您的 await
.
仅供参考,如果您对结果数组不感兴趣,则永远不要使用 .map()
returns,因为这就是它的全部意义。有更有效的方法来迭代数组。而且,在做异步事情时,现在 await
的普通 for
循环要强大得多,因为您可以完全控制 .forEach()
和 .map()
的循环] 不提供,因为他们 async
不知道。
您没有显示太多实际代码,但这是 for
循环的一般思路:
async someFunction() {
let type=[];
let result=[];
for (let value of object) {
type.push(value["Data.type"]);
if(some condition){
// 1st condition
if(some condition){
// 2nd condition
if(some condition){
let path= await functionName();
// 3rd conditon
if(some condtion){
// 4th condition
result.push("task");
}
}
}
}
}
console.log(result);
}
someFunction().then(() => {
console.log("all done");
}).catch(err => {
console.log(err);
});