1:匹配字符串跟数组中存在相同值,并取出数组中的对应元素
var strA = "1,2,3,4,5"; var strB = [{ "id":"2", "name":"小王" },{ "id":"5", "name":"小李" },{ "id":"8", "name":"小孙" },{ "id":"11", "name":"小赵" },{ "id":"22", "name":"小钱" }, ]
var obj={}; for(var i in strB){ obj[strB[i].id] = strB[i]; } var strArr = strA.split(","); console.log(strArr instanceof Array); var newArr = []; for(var item in strArr){ if(obj[item]){ newArr.push(obj[item]) } }