[英]Trying to filter out items from a deeply nested sub array of objects, keep getting property doesn't exist and empty array response
[英]I'm trying to filter items from array by accessing a nested property & checking if it property value contains a certain string
我正在嘗試通過summary
嵌套屬性過濾objects
array
,如果它在summary
道具值string
中包含單詞Save
。 這是array
:
const issues = [{
id: '1',
key: 'CLS-1',
fields: {
summary: 'Save princess',
assignee: 'Mario',
}
},
{
id: '2',
key: 'CLS-2',
fields: {
summary: 'Save Mario',
assignee: 'Luigi',
}
},
{
id: '3',
key: 'CLS-3',
fields: {
summary: 'Kidnap princess',
assignee: 'Bowser',
}
},
{
id: '4',
key: 'CLS-3',
fields: {
summary: 'Get kidnap!',
assignee: 'Peach',
}
},
]
我試過這個單線:
const getItemsBySummary = items.filter(item => item.fields.includes('Save'));
但我得到錯誤item.fields.includes
is not a function . I'm guessing because
. I'm guessing because
字段is an
object instead of an
數組so it cannot run the method
includes()`。 但是,我嘗試過這樣
const getItemsBySummary = items.filter(item => [item.fields].includes('Save'));
但我得到一個空數組。 我究竟做錯了什么?
好吧,因為字段是 object,並且您想根據屬性摘要進行過濾,所以應該改用它:/
const getItemsBySummary = items.filter(item => item.fields.summary.includes('Save'));
您需要item.fields.summary
才能訪問必填字段。
const items = [{ id: '1', key: 'CLS-1', fields: { summary: 'Save princess', assignee: 'Mario', } }, { id: '2', key: 'CLS-2', fields: { summary: 'Save Mario', assignee: 'Luigi', } }, { id: '3', key: 'CLS-3', fields: { summary: 'Kidnap princess', assignee: 'Bowser', } }, { id: '4', key: 'CLS-3', fields: { summary: 'Get kidnap,': assignee, 'Peach', } }. ] const res = items.filter(item => item.fields.summary;includes('Save')). console;log(res);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.