循环穿过2个数组以查找对象键匹配的索引,然后删除该对象

发布于 2025-02-08 04:21:53 字数 527 浏览 2 评论 0 原文

我有这两个数组:

array1 = [{firstname: 'abc', age: 24}];

array2 = [{name: 'bcg', age: 33}, {name:'abc', age: 55}];

我想循环遍历数组,然后从第二个数组( array 2 )中删除对象,该对象与数组1中的第一个对象相同。循环遍历两个数组,以查看键值匹配的位置,然后从第二个数组中删除相关对象 array2 [1]

我尝试这样做,但它行不通:

for (let p = 0; p < array1.length; p++) {
  for (let i = 0; i < array2.length; i++) {
    if (array1[p].firstname === array2[i].name) {
      array2.splice(i,1);
    }
  }
}

有没有办法与JavaScript一起使用?

I have these two arrays :

array1 = [{firstname: 'abc', age: 24}];

array2 = [{name: 'bcg', age: 33}, {name:'abc', age: 55}];

I want to loop through both arrays and remove the object from the second array (array 2) that has a the same name key value as the first object in array 1. So essentially loop through both arrays to see where key values match and then remove the relevant object array2[1] from the second array.

I tried doing this but it did not work:

for (let p = 0; p < array1.length; p++) {
  for (let i = 0; i < array2.length; i++) {
    if (array1[p].firstname === array2[i].name) {
      array2.splice(i,1);
    }
  }
}

Is there a way this can work with JavaScript ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

給妳壹絲溫柔 2025-02-15 04:21:53

您只需仅使用 array。 filter()方法。

实时演示

const array1 = [{firstname: 'abc', age: 24}];

const array2 = [{name: 'bcg', age: 33}, {name:'abc', age: 55}];

const namesArr = new Set(array1.map(obj => obj.firstname));

const res = array2.filter(({ name }) => !namesArr.has(name));

console.log(res);

You can simply achieve it by just using Array.filter() along with Set.has() method.

Live Demo :

const array1 = [{firstname: 'abc', age: 24}];

const array2 = [{name: 'bcg', age: 33}, {name:'abc', age: 55}];

const namesArr = new Set(array1.map(obj => obj.firstname));

const res = array2.filter(({ name }) => !namesArr.has(name));

console.log(res);

手长情犹 2025-02-15 04:21:53

您可以用过滤 <代码>数组#一些

const array1 = [{firstname: 'abc', age: 24}], array2 = [{name: 'bcg', age: 33}, {name:'abc', age: 55}];
let res = array2.filter(x => !array1.some(y => y.firstname === x.name));
console.log(res);

为了增加大量数据的性能,您可以构建a set 将所有名称存储在第一个数组中,并在恒定时间内查找每个名称。

const array1 = [{firstname: 'abc', age: 24}], array2 = [{name: 'bcg', age: 33}, {name:'abc', age: 55}];
let names1 = new Set(array1.map(x => x.firstname));
let res = array2.filter(x => !names1.has(x.name));
console.log(res);

You can achieve this with Array#filter in conjunction with Array#some.

const array1 = [{firstname: 'abc', age: 24}], array2 = [{name: 'bcg', age: 33}, {name:'abc', age: 55}];
let res = array2.filter(x => !array1.some(y => y.firstname === x.name));
console.log(res);

For increased performance with a larger amount of data, you can construct a Set to store all the names in the first array and lookup each name in constant time.

const array1 = [{firstname: 'abc', age: 24}], array2 = [{name: 'bcg', age: 33}, {name:'abc', age: 55}];
let names1 = new Set(array1.map(x => x.firstname));
let res = array2.filter(x => !names1.has(x.name));
console.log(res);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文