为什么我不能连接这两个数组?
Why can't I concatenate these two arrays?
这是代码(Vuex 突变):
export const CREATE_PANORAMAS = (state, panoramas) => {
console.log('building.panoramas:', state.building.panoramas)
console.log('panoramas:', panoramas)
state.building.panoramas.concat(panoramas)
console.log('result:', state.building.panoramas)
}
这是带有相应日志的结果:
[] // building.panoramas
[{ name: "", objectId: "5849133aac502e006c581b58" }] // panoramas
[] // result
为什么两个数组不串联?
我认为您需要先将 state.building.panoramas.concat(全景图) 分配给某些东西,或者您可以直接将其放在 'result' 行
sampleArray.concat(moreValues) returns 串联数组,它不会将 'moreValues' 串联到 sampleArray 本身。
From documentation - The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
使用 concat 的一种方法是执行以下操作
state.building.panoramas = state.building.panoramas.concat(panoramas)
或者您可以简单地
[].push.apply(state.building.panoramas, panoramas);
问题是您没有按照@JaromandaX 的指示将串联结果分配给 state.building.panoramas
数组。
您还可以使用剩余元素、展开元素、解构赋值来连接两个或多个数组
[...state.building.panoramas] = [...state.building.panoramas, ...panoramas];
这是代码(Vuex 突变):
export const CREATE_PANORAMAS = (state, panoramas) => {
console.log('building.panoramas:', state.building.panoramas)
console.log('panoramas:', panoramas)
state.building.panoramas.concat(panoramas)
console.log('result:', state.building.panoramas)
}
这是带有相应日志的结果:
[] // building.panoramas
[{ name: "", objectId: "5849133aac502e006c581b58" }] // panoramas
[] // result
为什么两个数组不串联?
我认为您需要先将 state.building.panoramas.concat(全景图) 分配给某些东西,或者您可以直接将其放在 'result' 行
sampleArray.concat(moreValues) returns 串联数组,它不会将 'moreValues' 串联到 sampleArray 本身。
From documentation - The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
使用 concat 的一种方法是执行以下操作
state.building.panoramas = state.building.panoramas.concat(panoramas)
或者您可以简单地
[].push.apply(state.building.panoramas, panoramas);
问题是您没有按照@JaromandaX 的指示将串联结果分配给 state.building.panoramas
数组。
您还可以使用剩余元素、展开元素、解构赋值来连接两个或多个数组
[...state.building.panoramas] = [...state.building.panoramas, ...panoramas];