搜索索引数组,如果在javascript

时间:2019-03-26 05:19:57

标签: javascript

我有一组数组:

var arr = [ 
[ 'AAAA.10', 'S10' ],
[ 'AAAA.10', 'S11' ],
[ 'AAAA.12', 'S12' ],
[ 'AAAA.13', 'S13' ],
[ 'AAAA.14', 'S14' ] ]

如果要找到任何匹配项,我想搜索整个数组值。

如果我像10一样进行搜索,则应检查是否找到匹配项(因此匹配项为AAAA.10),以检查数组值,然后应返回整个数组,如下所示:[[ 'AAAA.10', 'S10' ], [ 'AAAA.10', 'S11' ]]

我尝试了下面的代码,但是没有用:

console.log(arr.find('10'));

请帮助我实现输出。

8 个答案:

答案 0 :(得分:2)

您可以在.document嵌套数组中的第一项,并像这样使用split

let arr=[['AAAA.3','S3'],['AAAA.4','S4'],['AAAA.5','S5'],['AAAA.6','S6'],['AAAA.7','S7'],['AAAA.8','S8'],['AAAA.9','S9'],['AAAA.10','S10'],['AAAA.10','S11'],['AAAA.12','S12'],['AAAA.13','S13'],['AAAA.14','S14']]

let result = arr.filter(a => a[0].split('.')[1] === "10")

console.log(result)

如果您支持filter,则可以执行以下操作:

arr.filter(([key]) => key.split('.')[1] === "10")

更新

如果要搜索两个索引,则可以使用destructuring

var arr=[['AAAA.3','S3'],['AAAA.4','S4'],['AAAA.5','S5'],['AAAA.6','S6'],['AAAA.7','S7'],['AAAA.8','S8'],['AAAA.9','S9'],['AAAA.10','S10'],['AAAA.10','S11'],['AAAA.12','S12'],['AAAA.13','S13'],['AAAA.14','S14']]

function getResults(arr, search){
  return arr.filter(a => a[0].includes(search) || a[1].includes(search))
}

console.log(getResults(arr, "10"))
console.log(getResults(arr, "S1"))

答案 1 :(得分:2)

使用forEachfind

var arr = [
  ['AAAA.3', 'S3'],
  ['AAAA.4', 'S4'],
  ['AAAA.5', 'S5'],
  ['AAAA.6', 'S6'],
  ['AAAA.7', 'S7'],
  ['AAAA.8', 'S8'],
  ['AAAA.9', 'S9'],
  ['AAAA.10', 'S10'],
  ['AAAA.10', 'S11'],
  ['AAAA.12', 'S12'],
  ['AAAA.13', 'S13'],
  ['AAAA.14', 'S14']
];
var arr1 = [];

function a(str) {
  arr.forEach(e => {
    e.find(x => {
      if (x.includes(str)) {
        arr1.push(e)
      }
    })
  })
}
a('10');
console.log(arr1);

答案 2 :(得分:2)

您可以在每个嵌套数组上使用Array.find来检查它是否包含所需的字符串,然后使用Array.reduce累积结果:

var arr = [ [ 'AAAA.3', 'S3' ],
[ 'AAAA.4', 'S4' ],
[ 'AAAA.5', 'S5' ],
[ 'AAAA.6', 'S6' ],
[ 'AAAA.7', 'S7' ],
[ 'AAAA.8', 'S8' ],
[ 'AAAA.9', 'S9' ],
[ 'AAAA.10', 'S10' ],
[ 'AAAA.10', 'S11' ],
[ 'AAAA.12', 'S12' ],
[ 'AAAA.13', 'S13' ],
[ 'AAAA.14', 'S14' ] ];

function searchStr(search){
  const output = arr.reduce((acc, ele) => {
   let arr = ele.find(str => str.includes(search));
   return arr ? acc.concat(ele) : acc;
  }, []);  
  return output;
}
console.log(searchStr("10"));

答案 3 :(得分:1)

function checkNum(arr, num) {
    return arr.filter(ele => ele[0].split('.')[1] === String(num))
}
checkNum(arr, 10);

答案 4 :(得分:1)

您可以尝试以下代码:

var arr = [ [ 'AAAA.3', 'S3' ],
[ 'AAAA.4', 'S4' ],
[ 'AAAA.5', 'S5' ],
[ 'AAAA.6', 'S6' ],
[ 'AAAA.7', 'S7' ],
[ 'AAAA.8', 'S8' ],
[ 'AAAA.9', 'S9' ],
[ 'AAAA.10', 'S10' ],
[ 'AAAA.10', 'S11' ],
[ 'AAAA.12', 'S12' ],
[ 'AAAA.13', 'S13' ],
[ 'AAAA.14', 'S14' ] ];


function search(index){
  for(i = 0; i<=arr.length-1; i++){
    if(arr[i].indexOf(index)>-1){
      console.log(arr[i]);
    }
  }
 }
 
 search("AAAA.10");

答案 5 :(得分:1)

如果内部数组中的所有元素都是字符串值,则可以将它们连接起来并进行字符串搜索。

<h1>Our customers</h1>
<ul>

@foreach($cust as $cust1)
    <li>{{$cust1}}</li>
@endforeach

</ul>

如果您有对象或更喜欢嵌套循环,则也可以尝试如下操作:

var arr = [ [ 'AAAA.3', 'S3' ], [ 'AAAA.4', 'S4' ], [ 'AAAA.5', 'S5' ], [ 'AAAA.6', 'S6' ], [ 'AAAA.7', 'S7' ], [ 'AAAA.8', 'S8' ], [ 'AAAA.9', 'S9' ], [ 'AAAA.10', 'S10' ], [ 'AAAA.10', 'S11' ], [ 'AAAA.12', 'S12' ], [ 'AAAA.13', 'S13' ], [ 'AAAA.14', 'S14' ] ];

const searchValue = '10';

const output = arr.filter((item) => item.join('||').includes(searchValue) );
console.log(output)

答案 6 :(得分:0)

您可以使用filter返回一个新数组,并在filter内进行回调,以检查内部元素是否以匹配值结尾并为此使用endsWith

如果要获取includes includes在任何位置的元素数组,可以使用val

还请注意,如果嵌套数组中的两个元素都具有匹配字符,则将返回此值,但是如果您使用||而不是&&,则它还将返回['AAAA.10', 'S11']

var arr = [
  ['AAAA.3', 'S3'],
  ['AAAA.4', 'S4'],
  ['AAAA.5', 'S5'],
  ['AAAA.6', 'S6'],
  ['AAAA.7', 'S7'],
  ['AAAA.8', 'S8'],
  ['AAAA.9', 'S9'],
  ['AAAA.10', 'S10'],
  ['AAAA.10', 'S11'],
  ['AAAA.12', 'S12'],
  ['AAAA.13', 'S13'],
  ['AAAA.14', 'S14']
]

function findNestArray(val) {
  return arr.filter(function(item) {
    if (item[0].endsWith(val)) {
      return item
    }
  })
}
console.log(findNestArray('10'))

答案 7 :(得分:0)

尝试最简单的方法:

var result = arr.filter(function(item){
    return item[0].includes(10);
})

console.log(result);

过滤器方法检查数组的每个项目。然后根据给定函数的返回值返回该项目。 filter方法将每个项目作为参数传递给给定函数。如果函数返回true,则选择该项目。 include方法检查字符串是否包含10。您可以使用item[0].endsWith("10")在最后一个位置检查 10