英文:
how to find an array with a particular value and map to another array in Angular 8
问题
I can help you with the Chinese translation:
"我有一个名为 this.data 的数组,其中包含一个名为 platforms 的数组,而 platforms 中包含一个名为 sectionList 的数组(sectionList 是一个包含一些值的数组,如 sectionName、sectionid、sectionVal 等)。
我需要将新数组值(this.newSectionList)映射到旧的 sectionList 中的特定 sectionid。
我可以像这样映射 sectionList 中的每个值:
this.data.platforms.find(a => a.platformId === platformId).sectionList.find(f => f.sectionid === this.sectionid).sectionName = this.newSectionList.sectionName;
this.data.platforms.find(a => a.platformId === platformId).sectionList.find(f => f.sectionid === this.sectionid).sectionVal = this.newSectionList.sectionVal;
但我想将整个 sectionList 映射到 this.newSectionList。最简单的方法是什么?
下面的语法是错误的,但我该如何实现这一点?
this.data.platforms.find(a => a.platformId === platformId).sectionList.find(f => f.sectionid === this.sectionid) = this.newSectionList;
英文:
I have an array this.data that has an array of platforms and
platforms has an array of sectionlist
( sectionList is an array of few values like sectionName , sectionid, sectionVal etc. )
I need to map a new array value (this.newSectionList) to the old sectionList for a specific sectionid.
I can map each value within the the sectionList like this
this.data.platforms.find(a => a.platformId === platformId).sectionList.find(f => f.sectionid === this.sectionid).sectionName = this.newSectionList.sectionName;
this.data.platforms.find(a => a.platformId === platformId).sectionList.find(f => f.sectionid === this.sectionid).sectionVal = this.newSectionList.sectionVal;
Instead i want to map the entire sectionList to this.newSectionList . What is the easiest way?
This syntax below is wrong but how do i achieve this?
this.data.platforms.find(a => a.platformId === platformId).sectionList.find(f => f.sectionid === this.sectionid) = this.newSectionList;
答案1
得分: 1
I prefer step by step. The "key" is use the spread operator to give all the properties to the section
const platform = this.data.platforms.find(a => a.platformId === platformId)
const section = platform ?
platform.sectionList.find(f => f.sectionid === this.sectionid) : null
if (section)
section = { ...newSection }
else
platform.sectionList.push(newSection)
英文:
I prefer step by step. The "key" is use the spread operator to give all the properties to the section
const platform=this.data.platforms.find(a => a.platformId === platformId)
const section=platform?
platform.sectionList.find(f => f.sectionid ===this.sectionid):null
if (section)
section={...newSection}
else
platform.sectionList.push(newSection)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论