Javascript 从包含两个元素的数组中获取与匹配元素不匹配的值
我有两个元素的数组Javascript 从包含两个元素的数组中获取与匹配元素不匹配的值,javascript,arrays,Javascript,Arrays,我有两个元素的数组 var a = ['a','b']; 我有一个remover变量作为var remover='a' 我希望结果变量为var result='b' 我试过了 var current_id = 'windows123456'; var allIds = People.Ids(); // this will have ['windows123456','windows123'] var index = allIds.indexOf(current_id); if (inde
var a = ['a','b'];
我有一个remover变量作为var remover='a'
我希望结果变量为var result='b'
我试过了
var current_id = 'windows123456';
var allIds = People.Ids(); // this will have ['windows123456','windows123']
var index = allIds.indexOf(current_id);
if (index >= 0) {
allIds.splice( index, 1 );
}
console.log(allIds[0]); //windows123
是否有任何简单的方法可以做到这一点???如果只需要对两个元素数组执行此操作,则可以:
var result = a.find(item => item != remover);
如果需要使用较长的数组,则返回['b']
:
var result = a.filter(item => item != remover);
您可以分解阵列的结构:
var [remover,result]=a;