在 JQuery 中检查数组和打印变量的索引

Check index of an array and print variable in JQuery?

本文关键字:打印 变量 索引 数组 JQuery 检查      更新时间:2023-09-26

我有 2 个变量数组:

var a = [22, 34, 56,22]
var b = [red, blue, yellow, gray ]

我检查数组 a 的索引,其中有值 22 ,索引是03然后我打印var b[0]b[3],我得到RedGray

我如何使用/在 Jquery 中执行此操作?

你是这个意思吗?

  var a = [22, 34, 56, 22];
  var b = ["red", "blue", "yellow", "gray"];
  var input = 22;
  var matches = $.map(a, function (o, i) { //Using map to get the array of matches, apply map on a
      if (o === input) return b[i]; //check if the value is equal to input, if so return the value from array b for the same index
  })
  console.log(matches); //matches will be array of matches

小提琴