3 回答
TA贡献1804条经验 获得超7个赞
var arr = ['管理员','用户'];
var arr1 = [];
var originalData = [
{
name:'管理员',
code:'admin'
},
{
name:'用户',
code:'user'
},
{
name:'超管',
code:'super'
}
];
for(var i = 0, len = originalData.length; i < len; i++){
for(var j = 0, len1 = arr.length; j < len1; j++){
if(arr[j] == originalData[i].name){
arr1.push(originalData[i].code);
}
}
}
你说的是这个意思吗
TA贡献1810条经验 获得超4个赞
const data = [
{ name: '管理员', code: 'admin' },
{ name: '用户', code: 'user' },
{ name: '超管', code: 'super' }
]
const names = ['管理员', '用户']
let codes = []
data.forEach(dataItem => {
const { name, code } = dataItem
if (names.indexOf(name) !== -1) {
codes.push(code)
}
})
console.log(codes)
TA贡献1875条经验 获得超3个赞
const data = [
{ name: '管理员', code: 'admin' },
{ name: '用户', code: 'user' },
{ name: '超管', code: 'super' }
]
const names = ['管理员', '用户']
let codes = data.filter(({name})=>names.indexOf(name)!==-1).map(person=>person.code)
添加回答
举报