如何通过一组两个或多个数组进行映射以获得长度结果?
How do I map through group of two or more Arrays to get a length result?
我有两个或更多带输入的数组
this.listNumber = [
{
"GenericQuestions": [
{
"input": "long",
},
{
"input": "dog",
},
{
"input": "",
}
],
},
{
"GenericQuestions": [
{
"input": "fred",
},
{
"input": "barney",
},
{
"input": "betty",
}
]
}
]
// can be more Arrays
我之前使用过此 getFilter 方法来获取具有来自单个数组的值的输入结果
getFilter(index: string | number) { // index being 0
return this.listNumber[index].GenericQuestions.filter((c: { input: any }) => !!c.input).length;
} // returns 2
我想弄清楚的是如何获得从两个数组填写的输入结果。
我什至试过了
const flatfile = this.listNumber.flat();
console.log("flatfile", flatfile);
但我认为它失败了,因为 GenericQuestions 在单独的对象中。
假设您的数组 listNumbers
如下所示:
this.listNumbers = [
{
"GenericQuestions": [
{
"input": "some text 1"
},
{
"input": "some text 2"
},
{
"input": "some text 3"
}
]
},
{
"GenericQuestions": [
{
"input": "some text 4"
},
{
"input": "some text 5"
},
{
"input": "some text 6"
}
]
}
]
要从两个数组 ("GenericQuestions"
) 获取输入字段,您可以创建一个新数组并用所有 "GenericQuestions"
个数组的元素填充它
const newArray = []
for (let object of this.listNumbers) {
newArray.push(...object["GenericQuestions"])
}
这将为您提供一个平面数组,其中包含所有输入对象
newArray = [
{
"input": "some text 1"
},
{
"input": "some text 2"
},
{
"input": "some text 3"
},
{
"input": "some text 4"
},
{
"input": "some text 5"
},
{
"input": "some text 6"
}
]
如果我没猜错的话。下面的代码是解决方案
let listNumber = [{
GenericQuestion: [{ input: "long" }, { input: "dog"}, { input: ""}]
},
{
GenericQuestion: [ {input: "fred"}, { input: "barney"}, { input: "betty" } ]
}];
let result = listNumber.map(x => x.GenericQuestion).flat().filter(t=>!!t.input);
console.log(result);
输出:
[
{ input: 'long' },
{ input: 'dog' },
{ input: 'fred' },
{ input: 'barney' },
{ input: 'betty' }
]
基本上,您只需要从 listNumber
数组中总结出 non-empty input
的数量。对吗?
那么你需要做的就是……嗯……总结一下:
let total_cnt = 0
for( let i = 0 ; i !== this.listNumber.length ; ++i ) total_cnt += this.getFilter(i)
console.log(total_cnt)
或者如果您更喜欢实用风格:
const total_cnt = this.listNumber.reduce((total_cnt, _, i) => total_cnt + this.getFilter(i), 0)
不需要创建临时数组,也不需要重写getFilter
已经测试过的逻辑。干!
this.listNumber = [
{
"GenericQuestions": [
{
"input": "long",
},
{
"input": "dog",
},
{
"input": "",
}
],
},
{
"GenericQuestions": [
{
"input": "fred",
},
{
"input": "barney",
},
{
"input": "betty",
}
]
}
]
// can be more Arrays
我之前使用过此 getFilter 方法来获取具有来自单个数组的值的输入结果
getFilter(index: string | number) { // index being 0
return this.listNumber[index].GenericQuestions.filter((c: { input: any }) => !!c.input).length;
} // returns 2
我想弄清楚的是如何获得从两个数组填写的输入结果。
我什至试过了
const flatfile = this.listNumber.flat();
console.log("flatfile", flatfile);
但我认为它失败了,因为 GenericQuestions 在单独的对象中。
假设您的数组 listNumbers
如下所示:
this.listNumbers = [
{
"GenericQuestions": [
{
"input": "some text 1"
},
{
"input": "some text 2"
},
{
"input": "some text 3"
}
]
},
{
"GenericQuestions": [
{
"input": "some text 4"
},
{
"input": "some text 5"
},
{
"input": "some text 6"
}
]
}
]
要从两个数组 ("GenericQuestions"
) 获取输入字段,您可以创建一个新数组并用所有 "GenericQuestions"
个数组的元素填充它
const newArray = []
for (let object of this.listNumbers) {
newArray.push(...object["GenericQuestions"])
}
这将为您提供一个平面数组,其中包含所有输入对象
newArray = [
{
"input": "some text 1"
},
{
"input": "some text 2"
},
{
"input": "some text 3"
},
{
"input": "some text 4"
},
{
"input": "some text 5"
},
{
"input": "some text 6"
}
]
如果我没猜错的话。下面的代码是解决方案
let listNumber = [{
GenericQuestion: [{ input: "long" }, { input: "dog"}, { input: ""}]
},
{
GenericQuestion: [ {input: "fred"}, { input: "barney"}, { input: "betty" } ]
}];
let result = listNumber.map(x => x.GenericQuestion).flat().filter(t=>!!t.input);
console.log(result);
输出:
[
{ input: 'long' },
{ input: 'dog' },
{ input: 'fred' },
{ input: 'barney' },
{ input: 'betty' }
]
基本上,您只需要从 listNumber
数组中总结出 non-empty input
的数量。对吗?
那么你需要做的就是……嗯……总结一下:
let total_cnt = 0
for( let i = 0 ; i !== this.listNumber.length ; ++i ) total_cnt += this.getFilter(i)
console.log(total_cnt)
或者如果您更喜欢实用风格:
const total_cnt = this.listNumber.reduce((total_cnt, _, i) => total_cnt + this.getFilter(i), 0)
不需要创建临时数组,也不需要重写getFilter
已经测试过的逻辑。干!