如何从数组中获取值?
How can I get a value from an array?
我在 ASP.netCore 从我的 Api 获取信息
在 angular 我有这个 :
0: {Id: 3, Role_Name: 'ITAdmin'}
1: {Id: 4, Role_Name: 'Admin'}
2: {Id: 5, Role_Name: 'user'}
并想从我的 CurrenUser 的数组中获取值
像这样的数组或其他东西
var rolename = itdamin , admin , user
var id = 3, 4, 5
用于将rolename和RoleId分开显示
谢谢你帮助我:)
您可以使用 reduce
来处理您的数组并获取您想要的 ID 和规则:
// sourse array
const tmp = [{
Id: 3,
Role_Name: 'ITAdmin'
}, {
Id: 4,
Role_Name: 'Admin'
}, {
Id: 5,
Role_Name: 'user'
},
];
// default empty target object
const defaultList = {
ids: [],
roles: [],
};
// handler of your sourse array
const reducedObj = tmp.reduce((obj, item) => {
// getting id and role from array item
const {Id, Role_Name} = item;
// update list
return {
ids: [...obj.ids, Id],
roles: [...obj.roles, Role_Name]
};
}, defaultList);
// getting ids and roles from reduced object
const {ids, roles} = reducedObj;
console.log('ids: ', ids);
console.log('roles: ', roles);
更简单的方法:
// sourse array
const tmp = [{
Id: 3,
Role_Name: 'ITAdmin'
}, {
Id: 4,
Role_Name: 'Admin'
}, {
Id: 5,
Role_Name: 'user'
},
];
// default empty target arrays
const ids = [];
const roles = [];
// handler of your sourse array
tmp.map(item => {
// getting id and role from array item
const {Id, Role_Name} = item;
// update lists
ids.push(Id);
roles.push(Role_Name);
});
console.log('ids: ', ids);
console.log('roles: ', roles);
现在您可以找到打印 2 个数组的编辑代码
let id = []
let role = []
const input = [{
Id: 3,
Role_Name: 'ITAdmin'
}, {
Id: 4,
Role_Name: 'Admin'
}, {
Id: 5,
Role_Name: 'user'
}, ];
for (let index = 0; index < input.length; index++) {
id[index] = input[index].Id;
role[index] = input[index].Role_Name
}
console.log(id)
console.log(role)
我在 ASP.netCore 从我的 Api 获取信息 在 angular 我有这个 :
0: {Id: 3, Role_Name: 'ITAdmin'}
1: {Id: 4, Role_Name: 'Admin'}
2: {Id: 5, Role_Name: 'user'}
并想从我的 CurrenUser 的数组中获取值 像这样的数组或其他东西
var rolename = itdamin , admin , user
var id = 3, 4, 5
用于将rolename和RoleId分开显示
谢谢你帮助我:)
您可以使用 reduce
来处理您的数组并获取您想要的 ID 和规则:
// sourse array
const tmp = [{
Id: 3,
Role_Name: 'ITAdmin'
}, {
Id: 4,
Role_Name: 'Admin'
}, {
Id: 5,
Role_Name: 'user'
},
];
// default empty target object
const defaultList = {
ids: [],
roles: [],
};
// handler of your sourse array
const reducedObj = tmp.reduce((obj, item) => {
// getting id and role from array item
const {Id, Role_Name} = item;
// update list
return {
ids: [...obj.ids, Id],
roles: [...obj.roles, Role_Name]
};
}, defaultList);
// getting ids and roles from reduced object
const {ids, roles} = reducedObj;
console.log('ids: ', ids);
console.log('roles: ', roles);
更简单的方法:
// sourse array
const tmp = [{
Id: 3,
Role_Name: 'ITAdmin'
}, {
Id: 4,
Role_Name: 'Admin'
}, {
Id: 5,
Role_Name: 'user'
},
];
// default empty target arrays
const ids = [];
const roles = [];
// handler of your sourse array
tmp.map(item => {
// getting id and role from array item
const {Id, Role_Name} = item;
// update lists
ids.push(Id);
roles.push(Role_Name);
});
console.log('ids: ', ids);
console.log('roles: ', roles);
现在您可以找到打印 2 个数组的编辑代码
let id = []
let role = []
const input = [{
Id: 3,
Role_Name: 'ITAdmin'
}, {
Id: 4,
Role_Name: 'Admin'
}, {
Id: 5,
Role_Name: 'user'
}, ];
for (let index = 0; index < input.length; index++) {
id[index] = input[index].Id;
role[index] = input[index].Role_Name
}
console.log(id)
console.log(role)