我有以下对象:
prefix = 'some_string'
[prefix + i for i in mywords]
我想做的是通过此数据进行映射,其中名为“ CodedValues”的对象的索引为1,2和5,如下所示:
DOMAIN_ROLE:{
type: "Domain"
DomainName: "XYZ"
DomainCode: 21
Title: "People Role"
CodedValues:{
1: {Code: 1, Label: "Land"},
2: {Code: 2, Label: "Forest"},
3: {Code: 3, Label: "Public"},
4: {Code: 4, Label: "Single"},
5: {Code: 5, Label: "Private"}
}
}
问题在于映射时,“键”的值为“代码”而不是数字1,2,3 ... 似乎密钥变成了key = CodedValues.Code,我想在其中工作,因为key = 1,key = 2,key = 5
试图以最好的方式进行解释。任何建议都会有所帮助。
答案 0 :(得分:1)
let DOMAIN_ROLE = {
type: "Domain",
DomainName: "XYZ",
DomainCode: 21,
Title: "People Role",
CodedValues:{
1: {Code: 1, Label: "Land"},
2: {Code: 2, Label: "Forest"},
3: {Code: 3, Label: "Public"},
4: {Code: 4, Label: "Single"},
5: {Code: 5, Label: "Private"}
}
}
// you can use object destructuring to get the data by key.
const {1:first,2:second, 5:fifth} = DOMAIN_ROLE.CodedValues;
console.log(first,second, fifth)
//[first, second, fifth].map(()=>{})
答案 1 :(得分:0)
DOMAIN_ROLE.CodedValues[1,2,5]
实际上不是您所期望的(获取具有键1,2和5的项)。您必须获取所有密钥并对其进行过滤和使用:
Object.keys(DOMAIN_ROLE.CodedValues).filter(k => /* filter logic */).map(key => {
// Use key here: DOMAIN_ROLE[key]
});
如果您要基于某些逻辑过滤密钥,这将很有帮助。如果您要对键进行硬编码,为什么不直接在注释中使用['1', '2', '5'].map(key => { /**/ });
作为@str键。