如何在Angular 8中找到具有特定值的数组并映射到另一个数组

huangapple go评论75阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2023年4月11日 10:04:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定