基本上,我使用的是'FLD_STR_101‘值来检索我的文件。我有不同的字段,以'FLD_STR_‘开头,所以我不能将if语句建立在这个特定字段上。我想要做的是用这样的FLD_STR...something映射和检索字段
values[startWith('FLD_STR_')]因此,我将能够检查字段是否以FLD_STR_开头,然后我将能够根据每个字段的类型(文件、文本.)区分该字段。
这是我举的例子,你可以理解。我似乎不能像这样在数组中注入startsWith()。对如何做到这一点有什么线索吗?
const test =Object.entries(values['FLD_STR_101']).map((entry, key) =>( {
test: entry[0],
test2:key
}))发布于 2022-07-20 14:21:48
一个想法可以是
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);
发布于 2022-07-20 14:37:45
您已经快到了,您可以使用"startWidth“方法,但不直接尝试与数组方法过滤器相结合,它更干净、更易读。
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
}))https://stackoverflow.com/questions/73053215
复制相似问题