我有一個數(shù)組,對象看起來像這樣: let arr = [ { taxonomy: 'category', id: [ 10, 100 ] }, { taxonomy: 'post_tag', id: [ 20 ] }, ];我希望能夠在數(shù)組中推送一個新對象,如下所示: const object = { taxonomy: 'category', id: 30 }我想要的是檢查屬性值為'taxonomy'的數(shù)組中的對象是否已經(jīng)存在,如果要這樣做,我只想將新對象中的id添加到現(xiàn)有對象中。我知道如何檢查屬性是否已經(jīng)存在,但我不完全知道如何將新的id添加到數(shù)組。因此,添加以上對象將導(dǎo)致此數(shù)組: [ { taxonomy: 'category', id: [ 10, 100, 30 ] }, // 30 is added { taxonomy: 'post_tag', id: [ 20 ] }, ];如果尚不存在,則應(yīng)添加它。有人可以幫我嗎?
檢查對象的屬性是否存在,僅更新另一個屬性
慕田峪4524236
2021-04-25 06:21:33