简体   繁体   English

Javascript 对象数组; 从另一个中减去一个

[英]Javascript arrays of Objects; Subtract one from another

Put simply, I want to subtract one array from another.简单地说,我想从另一个数组中减去一个数组。

The arrays are arrays of objects.数组是对象的数组。 I understand I can cycle through one array and on each item, comparing values in the other array, but that just seems a little messy.我知道我可以循环遍历一个数组和每个项目,比较另一个数组中的值,但这似乎有点混乱。

Thanks for the help, hopefully this question isnt too basic, I have tried googling it with no luck :(感谢您的帮助,希望这个问题不是太基本,我试过用谷歌搜索它但没有运气:(

EDIT:编辑:

The Objects in the Arrays I wish to remove will have identical values but are NOT the same object (thanks @patrick dw).我希望删除的数组中的对象将具有相同的值但不是相同的对象(感谢@patrick dw)。 I am looking to completely remove the subset from the initial array.我希望从初始数组中完全删除子集。

This answer is copied from https://stackoverflow.com/a/53092728/7173655 , extended with a comment and a solution with objects.这个答案是从https://stackoverflow.com/a/53092728/7173655复制的,扩展了评论和对象解决方案。

The code filters array A. All values included in B are removed from A.代码过滤器数组 A。 B 中包含的所有值都从 A 中删除。

 const A = [1, 4, 3, 2] const B = [0, 2, 1, 2] console.log(A.filter(n => !B.includes(n)))

The same with objects:对象也一样:

 const A = [{id:1}, {id:4}, {id:3}, {id:2}] const B = [{id:0}, {id:2}, {id:1}, {id:2}] console.log(A.filter(a => !B.map(b=>b.id).includes(a.id)))

http://phpjs.org/functions/index http://phpjs.org/functions/index

There is no built-in method to do this in JavaScript. JavaScript 中没有内置方法可以做到这一点。 If you look at this site there are a lot of functions for arrays with similar syntax to PHP.如果您查看此站点,则有很多用于数组的函数与 PHP 的语法相似。

http://www.jslab.dk/library/Array http://www.jslab.dk/library/Array

This site has some js functions on "sets"这个站点有一些关于“sets”的js函数

I think you need the diff function.我认为您需要 diff 功能。

It should remove all values from list a, which are present in list b keeping their order.它应该从列表 a 中删除所有值,这些值出现在列表 b 中并保持它们的顺序。

  let a = [0, 2, 5, 6, 1];
  let b = [2, 6, 2, 5, 0];

  function arrayDiff() {
    for (i of b) {
      for (j of a) {
        if (i === j) {
          a.splice(a.indexOf(j), 1);
        }
      }
    }
    return a;
  }

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

相关问题 两个对象数组之间的Javascript减法 - Javascript Subtract between two arrays of objects 数组和对象-将值从一个“替换”到另一个 - Arrays & Objects - 'substituting' value from one to another 如何在 javascript 中按元素从另一个数组中减去一个数组 - How to subtract one array from another, element-wise, in javascript Javascript,迭代两个对象数组,并将一个项目从一个添加到另一个(如果不存在) - Javascript, Iterating through two arrays of objects and adding an item from one to another “if it doesn't exist” 用数组从另一个对象中减去一个对象 - Subtract one object from another one with in array 使用JavaScript中的一个函数从数组和对象中获取键 - Get keys from both arrays and objects using one function in JavaScript 如何从另一个字符串中“减去”一个字符串? - How to 'subtract' one string from another? Javascript 2个对象数组,从一个对象数组中获取一个值,并将其分配给另一个对象数组 - Javascript 2 arrays of objects , get a value from one array of objects and assign it to the other array of objects Javascript数组从数组中的每个元素中减去一个值 - Javascript arrays subtract a value from every element in array Javascript中来自数组的随机对象 - Random objects from Arrays in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM