![](/img/trans.png)
[英]In React how to get a property value of an object using another property value?
[英]How to get an object value inside another object by a property value in Javascript
我有一個這樣的 object
const test = {
'/blogs/architecture':
{
name: 'Architecture',
icon: 'archway',
iconCategory: 'fas',
slug: '/blogs/architecture'
}
}
現在我想找到 slug 字段,而我只有 name 字段。 如何通過名稱找到 slug? 請幫忙
嘗試Object.values()
與Array#find()
一起使用:
const test = { '/blogs/architecture': { name: 'Architecture', icon: 'archway', iconCategory: 'fas', slug: '/blogs/architecture' } }; const findSlugByName = name => { return Object.values(test).find(obj => obj.name === name)?.slug; } console.log(findSlugByName('Architecture'));
const test = { '/blogs/architecture': { name: 'Architecture', icon: 'archway', iconCategory: 'fas', slug: '/blogs/architecture' }, '/blogs/architecture2': { name: 'Architecture2', icon: 'archway', iconCategory: 'fas', slug: '/blogs/architecture' } } const searchName = 'Architecture'; let searchResult; const node = Object.values(test).forEach((node) => { if(node.name === searchName) { searchResult = node; } }); console.log(searchResult);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.