简体   繁体   English

查找和删除数组中的匹配值和对应值

[英]Finding and removing matching and corresponding values in an array

Here's a sample of the problem I'm having in JavaScript:这是我在 JavaScript 中遇到的问题的示例:

first array [1, 2, 3, 4, 5, 6, 7]第一个数组 [1, 2, 3, 4, 5, 6, 7]

second array [7, 8, 9, 4, 2, 5, 7]第二个数组 [7, 8, 9, 4, 2, 5, 7]

In this case, I need to be able to find and eliminate "4" and "7" from both arrays, eliminating both.在这种情况下,我需要能够从两个数组中找到并消除“4”和“7”,同时消除两者。 This is based on their location and matching value.这是基于它们的位置和匹配值。

I haven't been able to find anything other than eliminating matching values.除了消除匹配值之外,我找不到任何其他东西。 In this case, however, the values must be in the same place and also be matching.在这种情况下,但是,值必须在同一个地方,可以匹配。

I've tried this so far:到目前为止我已经尝试过这个:

function findCommonElements3(array1, array2) { 
  return arr1.some(item => arr2.includes(item)) 
} 

it looks like it only looks for matching elements, whereas I need to find matching corresponding elements and then remove them.看起来它只寻找匹配的元素,而我需要找到匹配的对应元素然后删除它们。

As mentioned in the comments, you may use the splice method to remove one or more elements of an array in JavaScript.正如评论中提到的,您可以使用splice方法删除 JavaScript 中数组的一个或多个元素。 First of all I would store the indexes of the elements I should remove looping the array as so:首先,我将存储元素的索引,我应该删除循环数组,如下所示:

const array1 = [1, 2, 3, 4, 5, 6, 7];
const array2 = [7, 8, 9, 4, 2, 5, 7];

//Indexes of same elements
var sameIndexes = [];

function findSameIndexes(element, index) {
  if (array1[index] == array2[index]) {
    sameIndexes.push(index);
  }
}

array1.forEach(findSameIndexes);

Calling console.log(sameIndexes) should give this result:调用 console.log(sameIndexes) 应该给出这个结果:

Array [3, 6]

The problem is that if you loop again the array and remove the elements in that order, the indexes would not correspond to the elements anymore.问题是,如果您再次循环数组并按该顺序删除元素,则索引将不再与元素对应。

For example if you remove the 3rd element, the number 7 wouldn't be at index 6 anymore, to solve this issue I'd use the reverse method so you won't lose track of the indexes例如,如果您删除第三个元素,数字 7 将不再位于索引 6 处,为了解决此问题,我将使用反向方法,这样您就不会丢失对索引的跟踪

// A simple function to remove the elements in both arrays
function removeElements(index) {
  array1.splice(index,1);
  array2.splice(index,1);
}

sameIndexes.reverse().forEach(removeElements);

And the final results would be最终结果将是

Array [1, 2, 3, 5, 6]
Array [7, 8, 9, 2, 5]

Which hopefully is what you were looking for, of course there are better ways to write it down, but maybe this will help you find a solution.希望这就是您正在寻找的内容,当然有更好的方法来写下来,但也许这会帮助您找到解决方案。

You could just use a for loop and use index.您可以只使用 for 循环并使用索引。 something like this像这样的东西

 const firstarray = [1, 2, 3, 4, 5, 6, 7] const secondarray = [7, 8, 9, 4, 2, 5, 7] for (let i = 0; i <= firstarray.length - 1; i++) { if (firstarray[i] === secondarray[i]) { console.log(`found ${firstarray[i]} at index ${i}`); firstarray.splice(i, 1); secondarray.splice(i, 1); } } console.log(firstarray, secondarray);

 const excludeCommon = (ar1, ar2) => { const both = [...ar1, ...ar2].filter((v, i, ar) => v !== ar[i + (2 * (i < ar1.length) - 1) * ar1.length]); return [both.slice(0, both.length / 2), both.slice(both.length / 2)]; } console.log(excludeCommon([1, 2, 3, 4, 5, 6, 7], [7, 8, 9, 4, 2, 5, 7]));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM