I have an object (values). I want to select the field which startWith('fld_str_)and which contains an object(for this case the FLD_STR_102). I want to delete the entire object from the createValue array and place it in an object CreateFileValue. You can check the output
Example input:
const values = {
ID: 748817,
createValues: [{
field: "FLD_STR_101",
value: 'hello',
},
{
field: "FLD_STR_102",
fileName: "doc.pdf",
value: {
field: 'FLD_STR_102',
fileName: 'bulletin_paie_avril.pdf',
value: 'jkhkjhkhkjh'
},
}
]
}
Example output:
const values = {
ID: 748817,
createValues: [{
field: "FLD_STR_101",
value: 'hello',
}
],
createFileValues: {
"field": "FLD_STR_102",
"fileName": "bulletin_paie_avril.pdf",
"value": "jkhkjhkhkjh"
}
}
const values = {
ID: 748817,
createValues: [{
field: "FLD_STR_101",
value: 'hello',
},
{
field: "FLD_STR_102",
fileName: "doc.pdf",
value: {
field: 'FLD_STR_102',
fileName: 'bulletin_paie_avril.pdf',
value: 'jkhkjhkhkjh'
},
}
]
}
const res = () => {
if (values.createValues.startsWith('FLD_STR_') && typeof values.createValues.startsWith('FLD_STR_') === 'object') {
return {
};
}
}
};
console.log(res());
CodePudding user response:
const values = {
ID: 748817,
createValues: [{
field: "FLD_STR_101",
value: 'hello',
},
{
field: "FLD_STR_102",
fileName: "doc.pdf",
value: {
field: 'FLD_STR_102',
fileName: 'bulletin_paie_avril.pdf',
value: 'jkhkjhkhkjh'
},
}
]
}
// build createFileValues structure
values.createFileValues = values.createValues.filter(v => v.field.startsWith("FLD_STR_") && typeof v.value === "object");
// filter the files out of createValues
values.createValues = values.createValues.filter(v => !values.createFileValues.includes(v));
console.log(values);
CodePudding user response:
values.createValues.startsWith('FLD_STR_')
isn't valid because createValues
is an array. You'll need to iterate over values.createValues
in order to check each field.startsWith('FLD_STR_')
and if value is an object (see How to detect if a variable is a pure javascript object for an example of this).
const values = {
ID: 748817,
createValues: [{
field: "FLD_STR_101",
value: 'hello',
},
{
field: "FLD_STR_102",
fileName: "doc.pdf",
value: {
field: 'FLD_STR_102',
fileName: 'bulletin_paie_avril.pdf',
value: 'jkhkjhkhkjh'
}
}
]
}
const res = () => {
var newResult = {
ID: values.ID,
createValues: []
};
values.createValues.forEach(function(item) {
var newCreateValue = {};
if (item.field.startsWith('FLD_STR_') && item.value.constructor.name === "Object") {
newCreateValue.field = item.value.field;
newCreateValue.value = item.value.value;
newCreateValue.fileName = item.value.fileName;
// if this is a createFilesValue, add it as a new key/value to the newResult object
newResult.createFileValues = newCreateValue;
} else {
newCreateValue.field = item.field;
newCreateValue.value = item.value;
// if this is an existing createValues, add it to the createValues array
newResult.createValues.push(newCreateValue);
}
});
return newResult;
};
console.log(res());
CodePudding user response:
This should work
const values = {
ID: 748817,
createValues: [{
field: "FLD_STR_101",
value: 'hello',
},
{
field: "FLD_STR_102",
fileName: "doc.pdf",
value: {
field: 'FLD_STR_102',
fileName: 'bulletin_paie_avril.pdf',
value: 'jkhkjhkhkjh'
},
}
]
}
const res = (data) => {
let oldCreateValues = data.createValues;
let oldCreateFileValues = data.createFileValues;
let newCreateValues = [];
let newCreateFileValues = oldCreateFileValues && Array.isArray(oldCreateFileValues) ? oldCreateFileValues : [];
if (oldCreateValues && Array.isArray(oldCreateValues)) {
oldCreateValues.forEach(item => {
if (item.field.startsWith('FLD_STR_') && item.value && typeof item.value === 'object') {
newCreateFileValues.push(item);
} else {
newCreateValues.push(item);
}
});
}
data.createValues = newCreateValues;
data.createFileValues = newCreateFileValues;
return data;
};
console.log(res(values));