Basically, I am selecting part of the object (values) and I want to inject CreateValue to this object, but with certain condition. I want to check if the field starts with a 'FLD_STR_' and if this field contain an object. I have multiple field 'fld_str_' hence the reason I want to only select the one that contains an object and not a string. I do not think I need to loop in this case. Just add a condition
const values = {
ID: 748817,
CODE: "OFFLINE-003",
FLD_STR_101: {
field: "FLD_STR_101",
name: "doc.pdf",
value: 'base64',
type: "application/pdf"
},
FLD_STR_102: "coucou",
Table: 109414,
OWNER: "MJACQUIER",
}
const ele = ()=>{
if (values.startsWith('FLD_STR_') &&
typeof values.startsWith('FLD_STR_') === 'object') {
values.createFileValue = values.startsWith('FLD_STR_');
}
}
console.log(ele())
this is the output :
const values = {
ID: 748817,
CODE: "OFFLINE-003",
FLD_STR_101: {
field: "FLD_STR_101",
name: "doc.pdf",
value: 'base64',
type: "application/pdf"
},
FLD_STR_102: "coucou",
Table: 109414,
OWNER: "MJACQUIER",
"createFileValue": {
field: "FLD_STR_101",
name: "doc.pdf",
value: 'base64',
type: "application/pdf"
}
}
what does values.startsWith('FLD_STR_') ? values is object not a string, try this
const keys = Object.keys(values);
keys.forEach(key => {
if(key.startsWith('FLD_STR_') && typeof values[key] === 'object'){
values.createFileValue = values[key];
}
})