我在下面有這個資料。我需要能夠在物件中搜索 id 或 name 鍵,然后將“顯示”鍵更改為不同的值。如何示例:
在資料中搜索 id 值 2 并將顯示值更改為 false。
data = [
{
id: 1,
name: 'one',
show: false;
title: 'title1',
data: [
{
id: 1,
description: 'some description'
},
{
id: 2,
description: 'some other description'
}
]
},
{
id: 2,
name: 'two',
show: true;
title: 'title2',
data: [
{
id: 1,
description: 'some description'
},
{
id: 2,
description: 'some other description'
}
]
}
]
我怎樣才能做到這一點?
uj5u.com熱心網友回復:
您可以使用該findIndex方法,然后使用找到的索引訪問您的陣列并更改您想要的任何屬性,這里有一些與您的用例相匹配的代碼
let index = data.findIndex((x) => x.id == THE_ID_YOU_ARE_LOOKING_FOR);
if(index > -1) {
data[index].show = THE_VALUE_YOU_WANT;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/497960.html
標籤:javascript 有角度的 打字稿
