JavaScript对象没有';突变
我想将我的对象的属性:JavaScript对象没有';突变,javascript,Javascript,我想将我的对象的属性:lists\u to\u download:{10920:“10920”}转换为lists\u to\u download:[“10920”],所以我创建了一个处理器类,它处理这种类型的操作,但它不会改变我的输入对象,我不知道问题出在哪里。查看下面的代码,并查看那里的控制台.log输出 class ObjectKeysToArraySettingsProcessor { constructor(objectPath = []) { this.objectPat
lists\u to\u download:{10920:“10920”}
转换为lists\u to\u download:[“10920”]
,所以我创建了一个处理器类,它处理这种类型的操作,但它不会改变我的输入对象,我不知道问题出在哪里。查看下面的代码,并查看那里的控制台.log
输出
class ObjectKeysToArraySettingsProcessor {
constructor(objectPath = []) {
this.objectPath = objectPath
}
_preProcessRecursion(part, objectPathIndex) {
if(_isArray(part) && objectPathIndex < this.objectPath.length - 1) {
part.forEach(item => {
this._preProcessRecursion(item, objectPathIndex + 1)
})
} else if(objectPathIndex === this.objectPath.length - 1) {
if(_isEmpty(part)) {
part = []
} else {
console.log(part) // it prints { 10920: "10920" }
part = _map(part, id => id)
console.log(part) // it prints ["10920"]
}
} else {
this._preProcessRecursion(
part[this.objectPath[objectPathIndex + 1]],
objectPathIndex + 1
)
}
}
preProcessSettings(settings) {
if(!_isEmpty(this.objectPath)) {
try {
this._preProcessRecursion(
settings[this.objectPath[0]],
0
)
} catch(err) {
console.log(err)
return settings
}
}
console.log(settings) // but here it prints lists_to_downloads: { 10920: "10920" } again...
return settings
}
}
类对象KeyArraySettingsProcessor{
构造函数(objectPath=[]){
this.objectPath=objectPath
}
_预递归(部分,objectPathIndex){
if(_isArray(part)&&objectPathIndex{
此.u预处理递归(项,objectPathIndex+1)
})
}else if(objectPathIndex==this.objectPath.length-1){
如果(_isEmpty(part)){
部分=[]
}否则{
log(part)//它打印{10920:“10920”}
part=\u映射(part,id=>id)
console.log(part)//打印[“10920”]
}
}否则{
这是递归(
部分[this.objectPath[objectPathIndex+1]],
objectPathIndex+1
)
}
}
预处理设置(设置){
如果(!\u为空(this.objectPath)){
试一试{
这是递归(
设置[this.objectPath[0]],
0
)
}捕捉(错误){
console.log(错误)
返回设置
}
}
log(settings)//但在这里它会打印列表到下载:{10920:“10920”}。。。
返回设置
}
}
解决方案是在递归的前一个/非最后一个步骤中进行转换
_preProcessRecursion(part, objectPathIndex) {
if(_isArray(part) && objectPathIndex < this.objectPath.length - 1) {
part.forEach(item => {
this._preProcessRecursion(item, objectPathIndex + 1)
})
} else if(objectPathIndex === this.objectPath.length - 2) {
const attribute = this.objectPath[objectPathIndex + 1]
if(_isEmpty(part[attribute])) {
part[attribute] = []
return
} else {
part[attribute] = _map(
part[attribute], id => id
)
}
} else {
this._preProcessRecursion(
part[this.objectPath[objectPathIndex + 1]],
objectPathIndex + 1
)
}
}
\u预处理递归(部分,objectPathIndex){
if(_isArray(part)&&objectPathIndex{
此.u预处理递归(项,objectPathIndex+1)
})
}else if(objectPathIndex==this.objectPath.length-2){
常量属性=this.objectPath[objectPathIndex+1]
if(_isEmpty(part[属性]){
部分[属性]=[]
回来
}否则{
部分[属性]=\u映射(
部分[属性],id=>id
)
}
}否则{
这是递归(
部分[this.objectPath[objectPathIndex+1]],
objectPathIndex+1
)
}
}
可能\u map(part,id=>id)
创建一个新数组,并将其分配给局部变量path
。但是对路径
的旧值的引用保留在设置
对象中。为了做您想要做的事情,您必须将设置
传递给函数并更改设置对象中的值。@RidgeA我不能将设置
传递给函数,我必须递归进行,因为对象路径可以是fex<代码>['parameters'、'films'、'additional''列出了要下载的内容],其中'films'可以是一个数组。我还教过对象是通过引用传递的,但无论如何,你不能按照你想要的方式来传递。JS不是这样工作的。您必须传递一个对象,该对象将包含一个引用另一个对象的属性,或者从函数返回一个新值并在外部重新分配它(根据您的示例,在预处理设置函数中)@RidgeA thx,我可能知道您的意思。const newKeys=object.keys(data).map(key=>data[key])