英文:
How can I return value(object) form filter method on Object.keys() instead of key?
问题
const notes = {
'jk2334|notes-md-23': {
id: 'notes-md-23',
text: 'First Note'
},
'jk2334|notes-xd-34': {
id: 'notes-xd-34',
text: 'Second Note'
},
'fd4345|notes-mf-54': {
id: 'notes-mf-54',
text: 'Third Note'
}
}
const result = Object.keys(notes).filter(note => {
if (note.startsWith('jk2334')) {
console.log('from inner -', notes[note]);
return notes[note];
}
})
console.log(result)
如果运行此代码,它仅返回键而不返回对象的值。但如果在条件内部使用console.log,则返回值。
我希望返回值的数组,而不是键。现在我该怎么做?
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const notes = {
'jk2334|notes-md-23': {
id: 'notes-md-23',
text: 'First Note'
},
'jk2334|notes-xd-34': {
id: 'notes-xd-34',
text: 'Second Note'
},
'fd4345|notes-mf-54': {
id: 'notes-mf-54',
text: 'Third Note'
}
}
const result = Object.keys(notes).filter(note => {
if(note.startsWith('jk2334')) {
console.log('from inner -', notes[note]);
return notes[note];
}
})
console.log(result)
<!-- end snippet -->
If I run this code it returns only key but not the value of the object. But if I console inside the condition it returns the value.
I want to return the array of value not key. What should I do now?
答案1
得分: 5
Array#filter
方法的回调函数应返回一个布尔值。如果要保留该值,则返回 true
,如果不需要该值,则返回 false
。不能使用该方法将输入值转换为不同的输出值。你的代码之所以偶然起作用,是因为对象是真值(truthy values)。
要将输入值转换为输出值,可以在过滤后调用 Array#map
方法,将键映射回值:
const notes = {
'jk2334|notes-md-23': {
id: 'notes-md-23',
text: 'First Note'
},
'jk2334|notes-xd-34': {
id: 'notes-xd-34',
text: 'Second Note'
},
'fd4345|notes-mf-54': {
id: 'notes-mf-54',
text: 'Third Note'
}
}
const result = Object.keys(notes)
.filter(note => note.startsWith('jk2334'))
.map(key => notes[key])
console.log(result)
以上是你提供的代码的翻译。
英文:
The callback to Array#filter
is expected to return a boolean value. true
if you want to keep the value and false
if you don't. You cannot use it to convert input values to different output values. The code you have works accidentally because objects are truthy values.
To convert input to output values you can call Array#map
after filtering to map the keys back to values:
<!-- begin snippet: js hide: false console: true babel: null -->
<!-- language: lang-js -->
const notes = {
'jk2334|notes-md-23': {
id: 'notes-md-23',
text: 'First Note'
},
'jk2334|notes-xd-34': {
id: 'notes-xd-34',
text: 'Second Note'
},
'fd4345|notes-mf-54': {
id: 'notes-mf-54',
text: 'Third Note'
}
}
const result = Object.keys(notes)
.filter(note => note.startsWith('jk2334'))
.map(key => notes[key])
console.log(result)
<!-- end snippet -->
答案2
得分: -1
你可以结合筛选和映射来缩小数组。
const
notes = { 'jk2334|notes-md-23': { id: 'notes-md-23', text: 'First Note' }, 'jk2334|notes-xd-34': { id: 'notes-xd-34', text: 'Second Note' }, 'fd4345|notes-mf-54': { id: 'notes-mf-54', text: 'Third Note' } },
result = Object
.entries(notes)
.reduce((r, [k, v]) => k.startsWith('jk2334') ? [...r, v] : r, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
英文:
You could combine filtering and mapping with reducing the array.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const
notes = { 'jk2334|notes-md-23': { id: 'notes-md-23', text: 'First Note' }, 'jk2334|notes-xd-34': { id: 'notes-xd-34', text: 'Second Note' }, 'fd4345|notes-mf-54': { id: 'notes-mf-54', text: 'Third Note' } },
result = Object
.entries(notes)
.reduce((r, [k, v]) => k.startsWith('jk2334') ? [...r, v] : r, []);
console.log(result);
<!-- language: lang-css -->
.as-console-wrapper { max-height: 100% !important; top: 0; }
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论