如何通过引用另一个数组来删除数组值

时间:2016-11-14 08:04:10

标签: javascript arrays

我有两个包含相同长度的数组:

var a = [a, b,c,d,e];
var b = [1,2,3,4,5];

我有另一个变量' C'其中包含一个数组a的值

var c  = "d"; 

如何删除' 4'在另一个阵列' b'基于var C的值。

所需的最终值:

finala = [a,b,c,e];
finalb = [1,2,3,5];
removeda = d;
removedb = 4; 

4 个答案:

答案 0 :(得分:1)

您可以使用Array#indexOf作为索引,并对两个数组使用Array#splice



protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);

// Retrieve list state and list/item positions
if(state != null)
   mListState =   mBundleRecyclerViewState.getParcelable(KEY_RECYCLER_STATE);
}

var a = ['a', 'b', 'c', 'd', 'e'],
    b = [1, 2, 3, 4, 5],
    c = 'd',
    index = a.indexOf(c),
    removeda = a.splice(index, 1)[0],
    removedb = b.splice(index, 1)[0];

console.log(a);
console.log(b);
console.log(removeda);
console.log(removedb);




答案 1 :(得分:0)

这应该做的工作

ng-if

答案 2 :(得分:0)

使用HMMACArray#splice方法。

var a = ['a', 'b', 'c', 'd', 'e'];
var b = [1, 2, 3, 4, 5];
var c = "d";

// get index of eleemnt in array `a`
var i = a.indexOf(c);

// remove element and store them in variable 
var removeda = a.splice(i, 1)[0],
  removedb = b.splice(i, 1)[0];

console.log(a, b, removeda, removedb)

答案 3 :(得分:0)

你走了。
从两个数组中删除元素,并存储删除的元素:

var a = ['a', 'b','c','d','e'],
    b = [1,2,3,4,5],
    c = "d",
    index = a.indexOf(c);

var removedA = a.splice(index, 1)[0];
var removedB = b.splice(index, 1)[0];
console.log(a);
console.log(removedA)
console.log(b);
console.log(removedB)