我试图匹配两个数组,如果它们包含相同的值,它将从数组之一中删除元素。我尝试使用splice()
方法。我尝试阅读,但是输出是:
new array ["SUGAR"]
但是,我想实现此输出:
["HYDROGENATED VEGETABLE↵OIL", "RAPESEED OIL", "PALM OIL", "SUNFLOWER OIL", "RAPESEED OIL", "PALM OIL"]
这是原始数组(unSafe
(:
["SUGAR", "HYDROGENATED VEGETABLE↵OIL", "RAPESEED OIL", "PALM OIL", "SUNFLOWER OIL", "RAPESEED OIL", "PALM OIL"]
这是我的代码:
function breakArray() {
let warning = this.navParams.get("warningResult");
let unSafe = this.navParams.get("unSafeResult");
console.log(unSafe);
console.log(warning);
for (var i = 0; i < warning.length; i++) {
for (var j = 0; j < unSafe.length; j++) {
if (warning[i] === unSafe[j]) {
console.log('warning', i);
console.log('unSafe', j);
unSafe.splice(j, 1);
console.log('new array', unSafe);
}
}
}
}
您需要确保j
为0。
// Original array.
var unsafe = ["SUGAR", "HYDROGENATED VEGETABLE↵OIL", "RAPESEED OIL", "PALM OIL", "SUNFLOWER OIL", "RAPESEED OIL", "PALM OIL"];
// Find the index of the item.
let removeIndexStart = unsafe.indexOf('SUGAR');
// Check if it exists.
if (removeIndexStart > -1) {
// Returns the removed items (from index 0, removing 1 item)
// and modifies the array in-place.
var removed = unsafe.splice(removeIndexStart, 1);
console.log('Removed:', JSON.stringify(removed, null, 2));
}
console.log('Modified:', JSON.stringify(unsafe, null, 2));
.as-console-wrapper { top: 0; max-height: 100% !important; }
尝试反向工作:
for (var i = warning.length - 1; i >= 0; i--) {
并在较大的数组上操作,这是您想要的。
warning.splice(i, 1);
let warning = ["SUGAR", "HYDROGENATED VEGETABLE↵OIL", "RAPESEED OIL", "PALM OIL", "SUNFLOWER OIL", "RAPESEED OIL", "PALM OIL"];
let unSafe = ["SUGAR"];
breakArray(); // I would pass in arguments.
console.log('New array:', warning);
function breakArray() {
for (var i = warning.length - 1; i >= 0; i--) {
for (var j = 0; j < unSafe.length; j++) {
if (warning[i] === unSafe[j]) {
console.log('warning', i);
console.log('unSafe', j);
warning.splice(i, 1);
}
}
}
}
.as-console-wrapper { top: 0; max-height: 100% !important; }
使用filter
方法而不是splice
。您不想在迭代的数组上拼接。
let unsafeArray = ["SUGAR", "HYDROGENATED VEGETABLE↵OIL", "RAPESEED OIL", "PALM OIL", "SUNFLOWER OIL", "RAPESEED OIL", "PALM OIL"];
let warningArray = ["SUGAR", "SUNFLOWER OIL"];
let filteredArray = unsafeArray;
warningArray.forEach(function (warningElement) {
filteredArray = filteredArray.filter(function (unsafeElement) {
return unsafeElement !== warningElement;
});
});
console.log(filteredArray);
似乎工作正常。返回没有"警告"元素的数组。
function breakArray() {
//let warning = this.navParams.get("warningResult");
//let unSafe = this.navParams.get("unSafeResult");
var warning = ["SUGAR"];
var unSafe = ["SUGAR", "HYDROGENATED VEGETABLE↵OIL", "RAPESEED OIL", "PALM OIL", "SUNFLOWER OIL", "RAPESEED OIL", "PALM OIL"];
//console.log(unSafe);
//console.log(warning);
for (var i = 0; i < warning.length; i++) {
for (var j = 0; j < unSafe.length; j++) {
if (warning[i] === unSafe[j]) {
//console.log('warning', i);
//console.log('unSafe', j);
unSafe.splice(j, 1);
console.log('new array', unSafe);
}
}
}
}
breakArray();