如何使用jQuery搜索JavaScript数组中的字符串?

时间:2016-12-24 13:28:54

标签: javascript jquery arrays

我有一个JavaScript数组:

var j_array = new Array();
j_arry=["class:1","division:a","class:5","class:3","division:b","division:c","division:d","class:10"];

我需要找到该类的次数及其数组键,所以我使用:

found = $.inArray('class', j_array); ` But it returns `-1`;

然后我用:

var search = 'class';
$.each([j_array], function(index, value){
    $.each(value, function(key, cell){
        if (search.indexOf(cell) !== -1)
            console.log('found in array '+index, cell);
    });
});

但这也是错误的。我该如何解决这个问题?

从这个数组我想得到以下内容:

  1. 班级来了4次,分别是0,2,3和7

  2. 我想创建一个单独的类数组,即

    new_array = ["class:1", "class:2", "class:3", "class:10"];
    
  3. 目前j_array中有四个班级。如何获得Nth class value

  4. 1st class value ="class:1"2nd class value="class:5"

7 个答案:

答案 0 :(得分:13)

您可以过滤在新数组中匹配的元素,并返回此新数组的长度

var j_arry = ["class:1","division:a","class:5","class:3","division:b","division:c","division:d","class:10"];
var res = j_arry.filter(x => x.includes("class"));
var key = res.map(x => x.split(":")[1]);
console.log("Class coming " + res.length + " , at key " + key.join(","));
console.log("new array = ", res);

答案 1 :(得分:8)

使用Array.prototype.filter过滤掉包含字符串 class 的数组元素 - 请参阅下面的演示:

var j_array =["class:1","division:a","class:5","class:3","division:b","division:c","division:d","class:10"];

var result = j_array.filter(function(e){
  return e.indexOf('class')!==-1;
});

console.log(result);

修改

要获取索引列表,您可以尝试:

var j_array =["class:1","division:a","class:5","class:3","division:b","division:c","division:d","class:10"];

var filteredIndices = []

var filtered = j_array.filter(function(e,i){
  if(e.indexOf('class')!==-1) {
    filteredIndices.push(i);
    return true;
  } else {
    return false;
  }
});

console.log(filtered);
console.log(filteredIndices);

// Nth class value
console.log(filtered[2]); // this prints the 3rd one
.as-console-wrapper{top:0;max-height:100%!important;}

答案 2 :(得分:3)

以下是您的问题1 + 2的答案。它也是'n'证明,所以也回答您的第3部分。这适用于老式的硬移植而不是时髦的功能。原始数组条目被拆分和过滤然后如果符合条件我们使用指针数组(列表)存储在关联数组(结果)中,以便更容易给出排序结果并从关联数组中提取值。 max变量可能不是必需的,但为了清楚起见包括在内 - 可能使用了list.length。请注意,list []数组将是稀疏的(缺少步骤),因此我们在输出步骤中使用之前测试每个条目。

		var j_array = new Array();
		j_arry=["class:1","division:a","class:5","class:3","division:b","division:c","division:d","class:10","class:1"];

		var a, result = [], list=[], max = -1    
		for (var i =0; i < j_arry.length; i = i + 1) {

			var a = j_arry[i].split(":")
		  if ( a[0] === "class") {
		  
				var key = "c" + a[1]
			  if ( !result[key] ) { result[key] = {pos:[]}}
				result[key].cnt = result[key].cnt ? result[key].cnt + 1 : 1; 
					result[key].pos.push(i)
					list[parseInt(a[1])] = "c" + a[1]  
			  
			  max = parseInt(a[1]) > max ? a[1] : max;
		  }
		}

		// say locations
		for (var i = 0; i < max; i = i + 1) {
			if (list[i]) {
			key = "c" + i 
				console.log("Class " + i + " occurs  at " + result[key].pos.toString()  )
		  }
		}

		// make new array
		var newArray=[]
		for (var i = 0; i < max; i = i + 1) {
			if (list[i]) {
				newArray.push("Class:" + i)
		  }
		}
		console.log("New array=" + newArray.toString()  )

结果是:

第1类发生在0,8 3级发生在3 第5类出现在2 新数组= Class:1,Class:3,Class:5

答案 3 :(得分:3)

这里单一减少就足够了。

var arr = ["class:1","division:a","class:5","class:3","division:b","division:c","division:d","class:10"],
    res = arr.reduce((p,c) => c.includes("class") ? (p.count++, p.keys.push(c.split(":")[1]), p)
                                                  : p ,{count:0, keys:[]});
console.log(res);

答案 4 :(得分:3)

您可以使用filtermap函数过滤数组,使其只包含与文本'class'匹配的元素,并使用数组索引表示法访问数组中的第n个元素。请查看以下代码段,希望对您有所帮助。

以下代码段使用ES6箭头语法。

&#13;
&#13;
var arr = ["class:1", "division:a", "class:5", "class:3", "division:b", "division:c", "division:d", "class:10"];
var result = arr.filter(x => x.indexOf('class') !== -1);
var indices = result.map(x => arr.indexOf(x));
console.log(indices);
console.log(result);

var nValue = window.prompt('Enter n value');
console.log(result[nValue]);
&#13;
&#13;
&#13;

答案 5 :(得分:3)

如果你使用jQuery来支持一些仍然没有实现新的Array函数的旧浏览器,并且你不想填充那些因为你已经在使用jQuery,那么你可以使用jQuery等价物:

var arr = ["class:1", "division:a", "class:5", "class:3", "division:b", "division:c", "division:d", "class:10"]
var result = $.grep(arr, function (x) { return x.indexOf('class') !== -1 })
var indices = $.map(result, function (x) { return arr.indexOf(x) })

这与this answer的代码相同,但使用的是jQuery。

答案 6 :(得分:1)

您必须先with然后signal a,b,GCD: integer; a<=100; --example of a b<=50; --example of b while NOT (a*b=0) loop if a>b then a<=a-b; else b<=b-a; end if; end loop; if a=0 then --result GCD<=b; else GCD<=a; end if;

map