如何从嵌套数组中获取最后一项?
How can I get the last items from a nested array?
我一直在努力从嵌套数组中获取最后一个对象项。我尝试实现了 flatMap、flat、filter 和 splice。但是,我仍然无法获得预期的数组。
const array = [
[
{
total_cases: 18,
},
{
total_cases: 32,
},
{
total_cases: 7,
},
],
[
{
total_cases: 4,
},
{
total_cases: 52,
},
],
[
{
total_cases: 68,
},
],
];
我有一个像上面这样的数组。如何只将最后一个对象项放入新数组?
预期的新数组应该是这样的:
[7, 52, 68]
提前致谢!
根据对问题的更改编辑。
您可以使用 map
and slice
to select only the last element of each nested array, then flat
来展平数组并再次使用 map
到 select total_cases
值:
const array = [
[
{ total_cases: 18 },
{ total_cases: 32 },
{ total_cases: 7 },
],
[
{ total_cases: 4 },
{ total_cases: 52 },
],
[
{ total_cases: 68 },
],
];
const result = array
.map(a => a.slice(a.length - 1))
.flat(1)
.map(o => o.total_cases)
console.log(result)
原答案
您可以flatten the array, filter
it based on the date
value and then use map
提取total_cases
值:
const array = [
[
{
date: "2020-02-25",
total_cases: 18,
},
{
date: "2020-02-26",
total_cases: 32,
},
{
date: "last-object",
total_cases: 7,
},
],
[
{
date: "2020-02-26",
total_cases: 4,
},
{
date: "last-object",
total_cases: 52,
},
],
[
{
date: "last-object",
total_cases: 68,
},
],
];
const result = array
.flat(1)
.filter(o => o.date == 'last-object')
.map(o => o.total_cases)
console.log(result)
您可以使用.at()方法。负参数从数组中的最后一项开始倒数。所以 .at(-1)
return 最后一个数组的元素。
const array=[[{total_cases:18},{total_cases:32},{total_cases:7}],[{total_cases:4},{total_cases:52}],[{total_cases:68}]];
const result = array.map(e => e.at(-1).total_cases);
console.log(...result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
我一直在努力从嵌套数组中获取最后一个对象项。我尝试实现了 flatMap、flat、filter 和 splice。但是,我仍然无法获得预期的数组。
const array = [
[
{
total_cases: 18,
},
{
total_cases: 32,
},
{
total_cases: 7,
},
],
[
{
total_cases: 4,
},
{
total_cases: 52,
},
],
[
{
total_cases: 68,
},
],
];
我有一个像上面这样的数组。如何只将最后一个对象项放入新数组?
预期的新数组应该是这样的:
[7, 52, 68]
提前致谢!
根据对问题的更改编辑。
您可以使用 map
and slice
to select only the last element of each nested array, then flat
来展平数组并再次使用 map
到 select total_cases
值:
const array = [
[
{ total_cases: 18 },
{ total_cases: 32 },
{ total_cases: 7 },
],
[
{ total_cases: 4 },
{ total_cases: 52 },
],
[
{ total_cases: 68 },
],
];
const result = array
.map(a => a.slice(a.length - 1))
.flat(1)
.map(o => o.total_cases)
console.log(result)
原答案
您可以flatten the array, filter
it based on the date
value and then use map
提取total_cases
值:
const array = [
[
{
date: "2020-02-25",
total_cases: 18,
},
{
date: "2020-02-26",
total_cases: 32,
},
{
date: "last-object",
total_cases: 7,
},
],
[
{
date: "2020-02-26",
total_cases: 4,
},
{
date: "last-object",
total_cases: 52,
},
],
[
{
date: "last-object",
total_cases: 68,
},
],
];
const result = array
.flat(1)
.filter(o => o.date == 'last-object')
.map(o => o.total_cases)
console.log(result)
您可以使用.at()方法。负参数从数组中的最后一项开始倒数。所以 .at(-1)
return 最后一个数组的元素。
const array=[[{total_cases:18},{total_cases:32},{total_cases:7}],[{total_cases:4},{total_cases:52}],[{total_cases:68}]];
const result = array.map(e => e.at(-1).total_cases);
console.log(...result);
.as-console-wrapper { max-height: 100% !important; top: 0; }