2数组中的JavaScript对象之间的差异 [英] Difference between two array of objects in JavaScript

查看:83
本文介绍了2数组中的JavaScript对象之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个结果,这样设置;

I have two result set like this;

// Result 1
[
    { value="4a55eff3-1e0d-4a81-9105-3ddd7521d642", display="Jamsheer" },
    { value="644838b3-604d-4899-8b78-09e4799f586f", display="Muhammed" },
    { value="b6ee537a-375c-45bd-b9d4-4dd84a75041d", display="Ravi" },
    { value="e97339e1-939d-47ab-974c-1b68c9cfb536", display="Ajmal" },
    { value="a63a6f77-c637-454e-abf2-dfb9b543af6c", display="Ryan" }
]

// Result 2
[
    { value="4a55eff3-1e0d-4a81-9105-3ddd7521d642", display="Jamsheer", $$hashKey="008" },
    { value="644838b3-604d-4899-8b78-09e4799f586f", display="Muhammed", $$hashKey="009" },
    { value="b6ee537a-375c-45bd-b9d4-4dd84a75041d", display="Ravi", $$hashKey="00A" },
    { value="e97339e1-939d-47ab-974c-1b68c9cfb536", display="Ajmal", $$hashKey="00B" }
]

最后的结果我需要的是这些阵列之间的差异即最后的结果showuld是这样

The final result I need is the difference between these arrays i.e the final result showuld be like this

[{ value="a63a6f77-c637-454e-abf2-dfb9b543af6c", display="Ryan" }]

是否有可能做这样的事情在JavaScript中?

Is it possible to do something like this in JavaScript?

推荐答案

使用唯一的本地JS,这样的事情可能工作:

Using only native JS, something like this could work:

a = [{ value:"4a55eff3-1e0d-4a81-9105-3ddd7521d642", display:"Jamsheer"}, { value:"644838b3-604d-4899-8b78-09e4799f586f", display:"Muhammed"}, { value:"b6ee537a-375c-45bd-b9d4-4dd84a75041d", display:"Ravi"}, { value:"e97339e1-939d-47ab-974c-1b68c9cfb536", display:"Ajmal"},  { value:"a63a6f77-c637-454e-abf2-dfb9b543af6c", display:"Ryan"}]
b = [{ value:"4a55eff3-1e0d-4a81-9105-3ddd7521d642", display:"Jamsheer", $$hashKey:"008"}, { value:"644838b3-604d-4899-8b78-09e4799f586f", display:"Muhammed", $$hashKey:"009"}, { value:"b6ee537a-375c-45bd-b9d4-4dd84a75041d", display:"Ravi", $$hashKey:"00A"}, { value:"e97339e1-939d-47ab-974c-1b68c9cfb536", display:"Ajmal", $$hashKey:"00B"}]

var onlyInA = a.filter(function(current){
    return b.filter(function(current_b){
        return current_b.value == current.value && current_b.display == current.display
    }).length == 0
});

var onlyInB = b.filter(function(current){
    return a.filter(function(current_a){
        return current_a.value == current.value && current_a.display == current.display
    }).length == 0
});

result = onlyInA.concat(onlyInB);

这篇关于2数组中的JavaScript对象之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆