首页 \ 问答 \ 比较两个JavaScript对象数组并删除常用数组对象(Comparing two JavaScript Arrays of Objects and removing common Array Objects)

比较两个JavaScript对象数组并删除常用数组对象(Comparing two JavaScript Arrays of Objects and removing common Array Objects)

var a = [
  [
    {
      id: "AAA"
    },
    {
      id: "BBB"
    }
  ],
  [
    {
      id: "AAA"
    },
    {
      id: "DDD"
    }
  ]
];
var b = [
  [
    {
      id: "BBB"
    }
  ],
  [
    {
      id: "CCC"
    },
    {
      id: "BBB"
    }
  ],
  [
    {
      id: "AAA"
    }
  ],
  [
    {
      id: "AAA"
    },
    {
      id: "DDD"
    }
  ],
  [
    {
      id: "DDD"
    }
  ],
  [
    {
      id: "CCC"
    },
    {
      id: "DDD"
    }
  ],
  [
    {
      id: "AAA"
    }
  ],
  [
    {
      id: "AAA"
    },
    {
      id: "BBB"
    }
  ]
];

function remove_duplicates(a, b) {
    for (var i = 0, len = a.length; i < len; i++) {
        for (var j = 0, len = b.length; j < len; j++) {
            if (a[i].name == b[j].name) {
                b.splice(j, 1);
            }
        }
    }

    console.log(a);
    console.log(b);

}

console.log(a);
console.log(b);

remove_duplicates(a,b);

我已经尝试了过滤器并减少了它们,但是它们以期望的结果来到了,我发现了一个类似的结构,但是我得到了一个不同的结构

从JavaScript或下划线寻求可能的解决方案

预期结果: [[{"id":"BBB"}],[{"id":"CCC"},{"id":"BBB"}],[{"id":"AAA"}],[‌​{"id":"DDD"}],[{"id"‌​:"CCC"},{"id":"DDD"}‌​]]


var a = [
  [
    {
      id: "AAA"
    },
    {
      id: "BBB"
    }
  ],
  [
    {
      id: "AAA"
    },
    {
      id: "DDD"
    }
  ]
];
var b = [
  [
    {
      id: "BBB"
    }
  ],
  [
    {
      id: "CCC"
    },
    {
      id: "BBB"
    }
  ],
  [
    {
      id: "AAA"
    }
  ],
  [
    {
      id: "AAA"
    },
    {
      id: "DDD"
    }
  ],
  [
    {
      id: "DDD"
    }
  ],
  [
    {
      id: "CCC"
    },
    {
      id: "DDD"
    }
  ],
  [
    {
      id: "AAA"
    }
  ],
  [
    {
      id: "AAA"
    },
    {
      id: "BBB"
    }
  ]
];

function remove_duplicates(a, b) {
    for (var i = 0, len = a.length; i < len; i++) {
        for (var j = 0, len = b.length; j < len; j++) {
            if (a[i].name == b[j].name) {
                b.splice(j, 1);
            }
        }
    }

    console.log(a);
    console.log(b);

}

console.log(a);
console.log(b);

remove_duplicates(a,b);

I have tried the filter and reduce but they are coming with desired result I found a similar one but the one I have got little different structure

seeking for possible solution from JavaScript or underscore

expected result: [[{"id":"BBB"}],[{"id":"CCC"},{"id":"BBB"}],[{"id":"AAA"}],[‌​{"id":"DDD"}],[{"id"‌​:"CCC"},{"id":"DDD"}‌​]]


原文:https://stackoverflow.com/questions/41689190
更新时间:2022-03-29 11:03

最满意答案

不,没有关于订单的保证。 它恰好在您的机器上以这种方式工作(例如,在我的计算机上ok并不总是如此)。


No, there are no guarantees about the order. It just happens to work this way on your machine(for instance, on my computer ok is not always true).

相关问答

更多
  • 为什么它更喜欢成员函数:: lock()和:: unlock()? 出于同样的原因,为什么RAII习惯用语在一般情况下变得流行起来(这只是它无数次的例子之一):因为你可以确定你没有离开当前范围而不解锁互斥体。 注意,这不仅仅是忘记调用unlock() :当你的互斥锁被锁定时,你的调用unlock()可能永远不会被触发,即使你的调用之间没有任何return语句lock()和您的电话unlock() 。 m.lock() // m is a mutex // ... foo(); // If this thro ...
  • 被取消的线程无法解锁它们持有的互斥锁是正确的,您需要安排这些手动发生,这可能会非常棘手,因为您需要非常小心地在每个可能的取消点周围使用正确的清理处理程序。 假设您使用pthread_cancel取消线程并使用pthread_cancel设置清理处理程序来解锁互斥体,那么您可以尝试一些替代方法,这可能会更简单,因此可能更加可靠。 使用RAII解锁互斥锁将更加可靠。 在GNU / Linux上, pthread_cancel是通过类型__cxxabi::__forced_unwind一个特殊的例外来__cxxa ...
  • 我不知道任何允许自动释放锁的pthreads API。 我个人尝试通过单个线程序列化对总线的访问,以避免这些并发症。 如果该线程维护一个输入队列而其他线程将其请求发布给它,则它可以执行它希望的任何序列化和冲突解决,这也使得在进一步访问之前更容易实现延迟。 只要你小心不要在这个线程中使用任何阻塞函数,你唯一真正的失败案例就是它可能会被意外终止。 如果您需要从总线读取和写入,则可以为每个线程提供一个输入队列,并让工作线程向IO线程发出“读取请求”,然后等待响应发布回其自己的输入队列。 如果只有一个未完成的线程请 ...
  • 如果您不将互斥锁锁定在更改条件和信号的代码路径中,则可能会失去唤醒。 考虑这一对过程: 流程A: pthread_mutex_lock(&mutex); while (condition == FALSE) pthread_cond_wait(&cond, &mutex); pthread_mutex_unlock(&mutex); 流程B(不正确): condition = TRUE; pthread_cond_signal(&cond); 然后考虑这种可能的指令交错,其中condition开 ...
  • 将这个条件包含在一个锁定函数中: bool condition() { mutex_lock(); bool result = ... mutex_unlock(); return result; } 然后在代码中使用 if () { ... } else if (condition(...)) { ... } Wrap up the condition in a function that does the locking: bool condition() { ...
  • 不,没有关于订单的保证。 它恰好在您的机器上以这种方式工作(例如,在我的计算机上ok并不总是如此)。 No, there are no guarantees about the order. It just happens to work this way on your machine(for instance, on my computer ok is not always true).
  • N2406中记录了此设计决策的基本原理 : 与boost不同,互斥锁具有lock(),unlock()等公共成员函数。这对于支持以下主要目标是必需的:用户定义的互斥锁可以与标准定义的锁一起使用。 如果用户定义的互斥体没有实现的接口,那么标准定义的锁将无法与用户定义的互斥体进行通信。 在编写时,boost :: mutex只能使用boost :: scoped_lock锁定和解锁。 所以你现在可以编写my::mutex ,并且只要你提供成员lock()和unlock() ,你的my::mutex和std::m ...
  • 你是怎么编译的? 我怀疑你没有将-pthread选项传递给编译器和pthread相关的东西,比如上面仍然是noops(即它们没有被拉入)。 我刚测试你编写的编程为 cc -pthread meh.c 并且结果在“锁定1”之后很好地挂起。 How are you compiling this? I suspect you did not pass the -pthread option to the compiler and pthread-related things like the above rema ...
  • 不,你不能自动锁定两个互斥锁。 此外,它看起来像是在一个线程中锁定互斥锁,然后在另一个线程中解锁它。 这是不允许的。 我建议切换到这个问题的条件变量。 请注意,将一个互斥锁与多个条件变量相关联是完全没问题的。 No, you can't atomically lock two mutexes. Additionally, it looks like you are locking a mutex in one thread and then unlocking it in another. That's n ...
  • 一个线程锁定ReentrantLock而不释放它。 “可重入”意味着您可以多次调用锁定,但必须调用unlock()次数相同。 你锁定两次,解锁一次,这样你实际上并没有解锁,因此没有其他进程有机会。 One thread is locking the ReentrantLock and not releasing it. What "reentrant" means is you can call lock many times, but you must call unlock() the same num ...

相关文章

更多

最新问答

更多
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)
  • 是否可以嵌套hazelcast IMaps?(Is it possible to nest hazelcast IMaps? And whick side effects can I expect? Is it a good Idea anyway?)
  • UIViewAnimationOptionRepeat在两个动画之间暂停(UIViewAnimationOptionRepeat pausing in between two animations)
  • 在x-kendo-template中使用Razor查询(Using Razor query within x-kendo-template)
  • 在BeautifulSoup中替换文本而不转义(Replace text without escaping in BeautifulSoup)
  • 如何在存根或模拟不存在的方法时配置Rspec以引发错误?(How can I configure Rspec to raise error when stubbing or mocking non-existing methods?)
  • asp用javascript(asp with javascript)
  • “%()s”在sql查询中的含义是什么?(What does “%()s” means in sql query?)
  • 如何为其编辑的内容提供自定义UITableViewCell上下文?(How to give a custom UITableViewCell context of what it is editing?)
  • c ++十进制到二进制,然后使用操作,然后回到十进制(c++ Decimal to binary, then use operation, then back to decimal)
  • 以编程方式创建视频?(Create videos programmatically?)
  • 无法在BeautifulSoup中正确解析数据(Unable to parse data correctly in BeautifulSoup)
  • webform和mvc的区别 知乎
  • 如何使用wadl2java生成REST服务模板,其中POST / PUT方法具有参数?(How do you generate REST service template with wadl2java where POST/PUT methods have parameters?)
  • 我无法理解我的travis构建有什么问题(I am having trouble understanding what is wrong with my travis build)
  • iOS9 Scope Bar出现在Search Bar后面或旁边(iOS9 Scope Bar appears either behind or beside Search Bar)
  • 为什么开机慢上面还显示;Inetrnet,Explorer
  • 有关调用远程WCF服务的超时问题(Timeout Question about Invoking a Remote WCF Service)