[英]trying to loop through inner object and changing the values based on condition
below is my object structure where i am trying to modify based on some values下面是我试图根据一些值修改的 object 结构
export const PROJECT_PHASE = Object.freeze({
CONCEPT: { label: 'Concept', stepNumber: 0, disabled: false, description: '' },
SCHEMATIC: { label: 'Schematic', stepNumber: 1, disabled: false, description: '' },
DESIGN_DEVELOPMENT: {
label: 'Design Development',
stepNumber: 2,
disabled: false,
description: ''
},
CONSTRUCTION_DOCUMENTS: {
label: 'Construction Documents',
stepNumber: 3,
disabled: false,
description: ''
}
});
and i am modifying the description
value based on condition and trying to return the same object structure but could not be able to get through this and getting an error like expression expected
at If statement.我正在根据条件修改description
值并尝试返回相同的 object 结构,但无法通过此操作并获得类似于 If 语句中expression expected
的错误。
Object.entries(PROJECT_PHASE).forEach(([key,value]) => (
if(value.stepNumber === currentStepNumber){
value.description = 'Current';
}
));
Could any please help on this, how can i achieve modify the inner object?任何人都可以帮助解决这个问题,我怎样才能修改内部 object? many thanks !!!!非常感谢 !!!!
You see that error because in your forEach you are using ()
instead of {}
您会看到该错误,因为在您的 forEach 中您使用的是()
而不是{}
PROJECT_PHASE=Object.freeze({ CONCEPT: { label: 'Concept', stepNumber: 0, disabled: false, description: '' }, SCHEMATIC: { label: 'Schematic', stepNumber: 1, disabled: false, description: '' }, DESIGN_DEVELOPMENT: { label: 'Design Development', stepNumber: 2, disabled: false, description: '' }, CONSTRUCTION_DOCUMENTS: { label: 'Construction Documents', stepNumber: 3, disabled: false, description: '' } }); currentStepNumber=3 Object.entries(PROJECT_PHASE).forEach(([key,value]) => { if(value.stepNumber === currentStepNumber){ value.description = 'Current'; } }); console.log(PROJECT_PHASE)
const PROJECT_PHASE = Object.freeze({ CONCEPT: { label: 'Concept', stepNumber: 0, disabled: false, description: '' }, SCHEMATIC: { label: 'Schematic', stepNumber: 1, disabled: false, description: '' }, DESIGN_DEVELOPMENT: { label: 'Design Development', stepNumber: 2, disabled: false, description: '' }, CONSTRUCTION_DOCUMENTS: { label: 'Construction Documents', stepNumber: 3, disabled: false, description: '' } }); const currentStepNumber = 2; const alteredEntries = Object.entries(PROJECT_PHASE).map(([key,value]) => { let newValue = value; if(value.stepNumber === currentStepNumber){ newValue.description = 'Current'; } return [key,newValue] }); const alteredObject = Object.fromEntries(alteredEntries); console.log(alteredObject);
This will create a copy of the PROJECT_PHASE
object modified as you want.这将创建根据需要修改的PROJECT_PHASE
object 的副本。 Note the use of map
instead of forEach
and taking the result and feeding it into Object.fromEntries
注意使用map
而不是forEach
并将结果输入Object.fromEntries
You cab use this way, there is a error in your code.你cab用这种方式,你的代码有错误。 I am using currentStepNumber is 3 here我在这里使用 currentStepNumber 是 3
Object.entries(PROJECT_PHASE).forEach(([key,value]) => {
if(value.stepNumber === 3){
value.description = 'Current';
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.