猿问

如何判断数组的某一项和另外一个数组对象里面的值相等,然后在取出对应的值

var arr = [0,1,3,2]

var objArr = [

{abc:0,def:'你'},
{abc:1,def:'我'},
{abc:2,def:'他'},
{abc:3,def:'它'}

]
如何判断当arr的某个下标对应的值和objArr的abc的值相等了取出对应def对应的值,这个方法如何封装

eg:arr[0] ==> "你"
arr[2] ==> "它"


汪汪一只猫
浏览 3329回答 4
4回答

慕田峪4524236

&nbsp; &nbsp; var aa = function(arr,objArr){&nbsp; &nbsp; &nbsp; &nbsp; for(let i=0;i<objArr.length;i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(let o in objArr[i]){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(objArr[i][o]===arr){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(objArr[i]['def'])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; aa(arr[0],objArr)

白猪掌柜的

<!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; &nbsp; <meta http-equiv="X-UA-Compatible" content="ie=edge">&nbsp; &nbsp; <title>Document</title></head><body>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; var arr = [0,1,2,3];&nbsp; &nbsp; &nbsp; &nbsp; var objArr = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {abc:0,def:'你'},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {abc:1,def:'我'},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {abc:2,def:'他'},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {abc:3,def:'它'}&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; function checkFn(num) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objArr.forEach(function(item){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(item.abc == arr[num]){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(item.def);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; checkFn(0); // checkFn(0) ==> '你'&nbsp; &nbsp; </script></body></html>

繁星coding

题主最好先学习一下markdown。

弑天下

arr.forEach((key,index) => {&nbsp; &nbsp; objArr.forEach(item => {&nbsp; &nbsp; &nbsp; &nbsp; if(item.abc === key) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr[index] = item.def&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; })})console.log('arr[0]',arr[0])
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答