Basically I am using the values 'FLD_STR_101' to retrieve my file. I have different field starting with 'FLD_STR_' so I cannot base my if statement on this specific field. What I would like is to to do is map and retrieve the field with FLD_STR...something like this
values[startWith('FLD_STR_')]
so then I will be able to check if the field starts with FLD_STR_ and then I will be able to differentiate the field depending on the type of each field(file, text ...)
Here is what I have as an example so you can understand. It seems that I cannot inject the startsWith() inside the array like this. Any clue on how to achieve this ?
const test =Object.entries(values['FLD_STR_101']).map((entry, key) =>( {
test: entry[0],
test2:key
}))
An idea can be
const values = {
FLD_STR_101: {
test: 1,
type: 'type1'
},
FLD_STR_102: {
test2: 1,
type: 'type1'
},
FLD_STR_103_NO_TYPE: {
test2: 1
},
NOTFLD_STR_102: {
test3: 1
}
};
let test = [];
Object.keys(values)
.filter(key => key.startsWith('FLD_STR_') && values[key]['type'])
.forEach(filteredKey => {
test = [
...test,
...Object.entries(values[filteredKey]).map((entry, key) => ({
test: entry[0],
test2: key
}))]
});
console.log(test);
You were almost there, you can use an "startWidth" method, but not directly, try combining with the array method filter, it's cleaner and readable.
const values = {
FLD_STR_101: {
test: 1
},
FLD_STR_102: {
test2: 2
},
INVALID_STR_103: {
test3: 3
}
};
const startWith = (str, prefix) => {
return str.slice(0, prefix.length) === prefix;
}
const test = Object.entries(values)
.filter(([key]) => startWith(key, 'FLD_STR_'))
.map((entry, key) =>( {
test: entry[0],
test2:key
}))