猿问

关于数据的联调问题

原始数据格式:


[

 {

    name:管理员,

    code:admin

 },

 {

    name:用户,

    code:user

 },

 {

    name:超管,

    code:super

 }

]

通过筛选后可以获得一些结果值


['管理员','用户']

但是目前做修改后传给后台的参数需要是code的形式,也就是"admin,user"的结果值,请问这里该如何写循环遍历


慕斯王
浏览 397回答 3
3回答

三国纷争

var arr = ['管理员','用户'];var arr1 = [];var originalData = [&nbsp;{&nbsp; &nbsp; name:'管理员',&nbsp; &nbsp; code:'admin'&nbsp;},&nbsp;{&nbsp; &nbsp; name:'用户',&nbsp; &nbsp; code:'user'&nbsp;},&nbsp;{&nbsp; &nbsp; name:'超管',&nbsp; &nbsp; code:'super'&nbsp;}];for(var i = 0, len = originalData.length; i < len; i++){&nbsp; &nbsp;for(var j = 0, len1 = arr.length; j < len1; j++){&nbsp; &nbsp; &nbsp; &nbsp; if(arr[j] == originalData[i].name){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr1.push(originalData[i].code);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;}&nbsp;}你说的是这个意思吗

慕莱坞森

const data = [&nbsp; &nbsp; { name: '管理员', code: 'admin' },&nbsp; &nbsp; { name: '用户', code: 'user' },&nbsp; &nbsp; { name: '超管', code: 'super' }]const names = ['管理员', '用户']let codes = []data.forEach(dataItem => {&nbsp; &nbsp; const { name, code } = dataItem&nbsp; &nbsp; if (names.indexOf(name) !== -1) {&nbsp; &nbsp; &nbsp; &nbsp; codes.push(code)&nbsp; &nbsp; }})console.log(codes)

翻过高山走不出你

const data = [&nbsp; &nbsp; { name: '管理员', code: 'admin' },&nbsp; &nbsp; { name: '用户', code: 'user' },&nbsp; &nbsp; { name: '超管', code: 'super' }]const names = ['管理员', '用户']let codes = data.filter(({name})=>names.indexOf(name)!==-1).map(person=>person.code)
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答