jQuery - 从数组中排除任何值的元素

时间:2017-09-19 01:07:01

标签: javascript jquery arrays

使用jquery,如何在值数组中选择没有属性值的所有元素?

假设我有数组import pyaudio import numpy as np p = pyaudio.PyAudio() volume = 0.5 # range [0.0, 1.0] fs = 44100 # sampling rate, Hz, must be integer duration = 4.0 # in seconds, may be float f = 440.0 # sine frequency, Hz, may be float # generate samples, note conversion to float32 array samples = (np.sin(2*np.pi*np.arange(fs*duration)*f/fs)).astype(np.float32) # for paFloat32 sample values must be in range [-1.0, 1.0] stream = p.open(format=pyaudio.paFloat32, channels=1, rate=fs, output=True) # play. May repeat with different volume values (if done interactively) stream.write(volume*samples) stream.stop_stream() stream.close() p.terminate() 并输入

['a', 'b']

如何只选择值为<input value="a"> <input value="b"> <input value="c"> <input value="d"> c的广告?有没有d使用某种选择器的方法,所以我可以用它来代替each()函数?

2 个答案:

答案 0 :(得分:6)

使用$('input')选择所有输入,然后使用filter()

&#13;
&#13;
var arr = ['a', 'b'];

var inputs = $('input').filter((x,y) => !arr.includes($(y).val()));
console.log(inputs.length);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input value="a">
<input value="b">
<input value="c">
<input value="d">
&#13;
&#13;
&#13;

答案 1 :(得分:3)

您可以使用:not()伪选择器

let not = ['a', 'b'];
$(`input:not([value=${not[0]}],[value=${not[1]}])`).val(1);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<input value="a">
<input value="b">
<input value="c">
<input value="d">

替代地

let not = ['a', 'b'];
$(`input:not(${not.map(prop => `[value=${prop}]`)})`).val(1);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<input value="a">
<input value="b">
<input value="c">
<input value="d">