比较两个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
最满意答案
不,没有关于订单的保证。 它恰好在您的机器上以这种方式工作(例如,在我的计算机上
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 ...
-
取消锁定互斥锁的线程不会解锁互斥锁(Cancelling a thread that has a mutex locked does not unlock the mutex)[2023-12-30]
被取消的线程无法解锁它们持有的互斥锁是正确的,您需要安排这些手动发生,这可能会非常棘手,因为您需要非常小心地在每个可能的取消点周围使用正确的清理处理程序。 假设您使用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开 ...
-
如何在if-else阶梯中为特定条件锁定和解锁互斥锁?(How to put mutex lock & unlock for a specific condition in if-else ladder?)[2023-09-29]
将这个条件包含在一个锁定函数中: 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() { ... -
互斥锁定/解锁订单(Mutex lock/unlock order)[2024-03-08]
不,没有关于订单的保证。 它恰好在您的机器上以这种方式工作(例如,在我的计算机上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 ...
-
同时锁定两个互斥锁(Lock two mutex at same time)[2022-08-28]
不,你不能自动锁定两个互斥锁。 此外,它看起来像是在一个线程中锁定互斥锁,然后在另一个线程中解锁它。 这是不允许的。 我建议切换到这个问题的条件变量。 请注意,将一个互斥锁与多个条件变量相关联是完全没问题的。 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 ... -
尝试使用ReetrantLock锁定/解锁时的Java线程锁定(true)(Java thread lock when trying to lock/unlock with ReetrantLock(true))[2022-06-28]
一个线程锁定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 ...