Array.splice没有返回正确的输出

时间:2017-05-03 15:03:48

标签: javascript arrays typescript ionic2

我试图匹配两个数组,如果它们包含相同的值,它将从一个数组中删除该元素。我尝试使用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);
      }
    }
  }
}

3 个答案:

答案 0 :(得分:1)

使用filter方法代替splice方法。你不想拼接你正在迭代的数组。

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您需要确保j为0。

&#13;
&#13;
// 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));
&#13;
.as-console-wrapper { top: 0; max-height: 100% !important; }
&#13;
&#13;
&#13;

尝试反向工作:

for (var i = warning.length - 1; i >= 0; i--) {

操作更大的阵列,这就是你想要的。

warning.splice(i, 1);

&#13;
&#13;
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);
      }
    }
  }
}
&#13;
.as-console-wrapper { top: 0; max-height: 100% !important; }
&#13;
&#13;
&#13;

答案 2 :(得分:0)

似乎工作正常。返回没有“warning”元素的数组。

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();