Javascript 查找数组中对象的计数会更改数组本身
我有一个关于查找数组中对象的计数的问题。 我的数组是:Javascript 查找数组中对象的计数会更改数组本身,javascript,arrays,typescript,Javascript,Arrays,Typescript,我有一个关于查找数组中对象的计数的问题。 我的数组是: completePositions: any = [ { totalPositions: [ { position: "CNA", count: 2 }, { position: "LVN", count: 5 }, { position: "RNA", count: 8 } ] }, { totalPositions: [
completePositions: any = [
{
totalPositions: [
{ position: "CNA", count: 2 },
{ position: "LVN", count: 5 },
{ position: "RNA", count: 8 }
]
},
{
totalPositions: [
{ position: "CNA", count: 13 },
{ position: "LVN", count: 11 },
{ position: "RNA", count: 15 }
]
}
];
我试图在totalPositions数组中找到每个位置的总计数,因此我的最终数组将如下所示:
totalPositionsCount = [
{ position: "CNA", count: 15 },
{ position: "LVN", count: 16 },
{ position: "RNA", count: 23 }
];
我创建了一个函数,对“TotalPositions”数组中的每个位置求和,并将求和推送到TotalPositionScont。我的函数完成了这项工作,我得到了总计数,但它更改了父数组“completePositions”,并将“completePositions”的第一个“totalPositions”替换为“totalPositionsCount”数组。我试图创建“completePositions”的备份并将其用于计算,但第一个数组“totalPositions”还是被“TotalPositionScont”数组替换
我总结这一立场的职能是:
codeToShowTotalResources() {
for (let totPos of this.completePositions) {
for (let eachPos of totPos.totalPositions) {
console.log("eachPos", eachPos);
let postCountIndex = this.totalPositionsCount.findIndex(
pos => pos.position == eachPos.position
);
if (postCountIndex != -1) {
console.log("Already exists, sum up positions");
let positionCount = this.totalPositionsCount[postCountIndex].count + eachPos.count;
this.totalPositionsCount[postCountIndex].count = positionCount;
} else {
console.log("Add it for first time");
this.totalPositionsCount.push(eachPos);
}
}
}
}
如果我替换此.totalPositionScont[postCountIndex].count=positionCount使用this.totalPositionScont[postCountIndex]={position:eachPos.position,count:positionCount}编码>代码>然后它就可以正常工作了。我想知道我做错了什么,还是应该这样做
我已在中打开了一个示例项目
任何帮助都将非常有用:)当您执行此操作时:this.totalpositionscont.push(eachPos)
,eachPos
变量表示来自completedpositions
的条目。所以现在this.totalPositionsCount
和this.completePositions
都引用了同一个对象。如果修改该对象,更改将显示在两个位置,因为它是同一个对象
以下是使用和的替代方法:
const completePositions=[
{
职位总数:[
{位置:“CNA”,计数:2},
{位置:“LVN”,计数:5},
{位置:“RNA”,计数:8}
]
},
{
职位总数:[
{位置:“CNA”,计数:13},
{位置:“LVN”,计数:11},
{位置:“RNA”,计数:15}
]
}
];
常数总计=完成位置
.map(x=>x.totalPositions)
.减少((acc,总位置)=>{
totalPositions.forEach(({position,count})=>{
acc[位置]=(acc[位置]| 0)+计数;
})
返回acc;
}, {})
控制台日志(总计)代码>执行此操作时:this.totalpositionscont.push(eachPos)
,变量eachPos
表示来自completedpositions
的条目。所以现在this.totalPositionsCount
和this.completePositions
都引用了同一个对象。如果修改该对象,更改将显示在两个位置,因为它是同一个对象
以下是使用和的替代方法:
const completePositions=[
{
职位总数:[
{位置:“CNA”,计数:2},
{位置:“LVN”,计数:5},
{位置:“RNA”,计数:8}
]
},
{
职位总数:[
{位置:“CNA”,计数:13},
{位置:“LVN”,计数:11},
{位置:“RNA”,计数:15}
]
}
];
常数总计=完成位置
.map(x=>x.totalPositions)
.减少((acc,总位置)=>{
totalPositions.forEach(({position,count})=>{
acc[位置]=(acc[位置]| 0)+计数;
})
返回acc;
}, {})
控制台日志(总计)因此,当我编写let copyOfArray=parentArray时,它不是创建parentArray的副本,而是对parentArray的引用,我对副本数组所做的每个计算也会反映在父数组中。感谢您的回答和清晰的替代方法。正确。在这种情况下,copyOfArray和parentArray指向同一个数组,更改将反映在这两个位置。最简单的方法是通过:const newArray=[…oldArray]
。但是,请注意,如果数组包含对其他对象的引用,这些对象仍将被链接。换句话说,您将有两个可以独立修改的数组,但它们的内容可能仍然包含对公共/共享对象的引用。如果您需要创建嵌套数组和对象的完全独立副本,则有。感谢您的帮助:)因此,当我编写let copyOfArray=parentArray时,它不是创建parentArray的副本,而是对parentArray的引用,并且我对副本数组所做的每个计算也会反映在父数组中。感谢您的回答和清晰的替代方法。正确。在这种情况下,copyOfArray和parentArray指向同一个数组,更改将反映在这两个位置。最简单的方法是通过:const newArray=[…oldArray]
。但是,请注意,如果数组包含对其他对象的引用,这些对象仍将被链接。换句话说,您将有两个可以独立修改的数组,但它们的内容可能仍然包含对公共/共享对象的引用。如果您需要创建嵌套数组和对象的完全独立副本,有。谢谢您的帮助:)