JavaScript中“[0]”在“.filter()[0]”之后做什么?
我看到了这样一个函数:JavaScript中“[0]”在“.filter()[0]”之后做什么?,javascript,function,methods,filter,syntax,Javascript,Function,Methods,Filter,Syntax,我看到了这样一个函数: getDish(id: number): Dish { return DISHES.filter((dish) => (dish.id === id))[0]; } 但我不明白为什么它使用[0]后过滤功能?我的意思是filter的返回值看起来像什么,为什么它只指定索引0 编辑:这是菜品: 筛选器返回一个已筛选的数组。[0]访问该数组的第一个元素。您是否阅读了.filter文档,您知道filter返回的是什么吗?做什么?返回一个数组?var x=[1
getDish(id: number): Dish {
return DISHES.filter((dish) => (dish.id === id))[0];
}
但我不明白为什么它使用[0]后过滤功能?我的意思是filter的返回值看起来像什么,为什么它只指定索引0
编辑:这是菜品:
筛选器返回一个已筛选的数组。[0]访问该数组的第一个元素。您是否阅读了.filter文档,您知道filter返回的是什么吗?做什么?返回一个数组?var x=[1,2,3]是什么;y=x[0]处理数组时该怎么办?代码应使用“查找而不是筛选器”筛选器的返回值是什么样子?筛选器筛选数组并返回返回条件为true的对象数组。[0]访问数组中的第一个索引。这是一种错误的做法,因为数组可能是空的。假设所有使用您的代码的人都会正确使用它是一种错误的做法。在访问第一个索引之前,应该检查长度,因为如果id不存在,它将抛出一个对象未定义错误。@DerekLawrence如果找不到匹配项,则不会抛出任何错误。也就是说,代码可能应该使用Arrayfind@DerekLawrence…它将抛出一个对象未定义错误-为什么未定义应该是一个错误?没有扔东西。
export const DISHES: Dish[] = [
{
id: 0,
name: 'Uthappizza',
image: '/assets/images/uthappizza.png',
category: 'mains',
label: 'Hot',
price: '4.99',
featured: true,
description: 'A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.',
comments: [
{
rating: 5,
comment: "Imagine all the eatables, living in conFusion!",
author: "John Lemon",
date: "2012-10-16T17:57:28.556094Z"
},
{
rating: 4,
comment: "Sends anyone to heaven, I wish I could get my mother-in-law to eat it!",
author: "Paul McVites",
date: "2014-09-05T17:57:28.556094Z"
},
{
rating: 3,
comment: "Eat it, just eat it!",
author: "Michael Jaikishan",
date: "2015-02-13T17:57:28.556094Z"
},
{
rating: 4,
comment: "Ultimate, Reaching for the stars!",
author: "Ringo Starry",
date: "2013-12-02T17:57:28.556094Z"
},
{
rating: 2,
comment: "It's your birthday, we're gonna party!",
author: "25 Cent",
date: "2011-12-02T17:57:28.556094Z"
}
]
},
{
id: 1,
name: 'Zucchipakoda',
image: '/assets/images/zucchipakoda.png',
category: 'appetizer',
label: '',
price: '1.99',
featured: false,
description: 'Deep fried Zucchini coated with mildly spiced Chickpea flour batter accompanied with a sweet-tangy tamarind sauce',
comments: [
{
rating: 5,
comment: "Imagine all the eatables, living in conFusion!",
author: "John Lemon",
date: "2012-10-16T17:57:28.556094Z"
},
{
rating: 4,
comment: "Sends anyone to heaven, I wish I could get my mother-in-law to eat it!",
author: "Paul McVites",
date: "2014-09-05T17:57:28.556094Z"
},
{
rating: 3,
comment: "Eat it, just eat it!",
author: "Michael Jaikishan",
date: "2015-02-13T17:57:28.556094Z"
},
{
rating: 4,
comment: "Ultimate, Reaching for the stars!",
author: "Ringo Starry",
date: "2013-12-02T17:57:28.556094Z"
},
{
rating: 2,
comment: "It's your birthday, we're gonna party!",
author: "25 Cent",
date: "2011-12-02T17:57:28.556094Z"
}
]
},
{
id: 2,
name: 'Vadonut',
image: '/assets/images/vadonut.png',
category: 'appetizer',
label: 'New',
price: '1.99',
featured: false,
description: 'A quintessential ConFusion experience, is it a vada or is it a donut?',
comments: [
{
rating: 5,
comment: "Imagine all the eatables, living in conFusion!",
author: "John Lemon",
date: "2012-10-16T17:57:28.556094Z"
},
{
rating: 4,
comment: "Sends anyone to heaven, I wish I could get my mother-in-law to eat it!",
author: "Paul McVites",
date: "2014-09-05T17:57:28.556094Z"
},
{
rating: 3,
comment: "Eat it, just eat it!",
author: "Michael Jaikishan",
date: "2015-02-13T17:57:28.556094Z"
},
{
rating: 4,
comment: "Ultimate, Reaching for the stars!",
author: "Ringo Starry",
date: "2013-12-02T17:57:28.556094Z"
},
{
rating: 2,
comment: "It's your birthday, we're gonna party!",
author: "25 Cent",
date: "2011-12-02T17:57:28.556094Z"
}
]
},
{
id: 3,
name: 'ElaiCheese Cake',
image: '/assets/images/elaicheesecake.png',
category: 'dessert',
label: '',
price: '2.99',
featured: false,
description: 'A delectable, semi-sweet New York Style Cheese Cake, with Graham cracker crust and spiced with Indian cardamoms',
comments: [
{
rating: 5,
comment: "Imagine all the eatables, living in conFusion!",
author: "John Lemon",
date: "2012-10-16T17:57:28.556094Z"
},
{
rating: 4,
comment: "Sends anyone to heaven, I wish I could get my mother-in-law to eat it!",
author: "Paul McVites",
date: "2014-09-05T17:57:28.556094Z"
},
{
rating: 3,
comment: "Eat it, just eat it!",
author: "Michael Jaikishan",
date: "2015-02-13T17:57:28.556094Z"
},
{
rating: 4,
comment: "Ultimate, Reaching for the stars!",
author: "Ringo Starry",
date: "2013-12-02T17:57:28.556094Z"
},
{
rating: 2,
comment: "It's your birthday, we're gonna party!",
author: "25 Cent",
date: "2011-12-02T17:57:28.556094Z"
}
]
}
];