Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何比较两个数组中的对象并得到公共对象_Javascript_Arrays_Node.js_Lodash_Ramda.js - Fatal编程技术网

Javascript 如何比较两个数组中的对象并得到公共对象

Javascript 如何比较两个数组中的对象并得到公共对象,javascript,arrays,node.js,lodash,ramda.js,Javascript,Arrays,Node.js,Lodash,Ramda.js,大家好,我有两个数组 var elements = [{ "id": "id_1", "type": "input", "businesstype": { "type": "text" } }, { "type": "label", "id": "id_234" }, { "id": "id_16677", "type": "div", },

大家好,我有两个数组

var elements = [{
        "id": "id_1",
        "type": "input",
        "businesstype": { "type": "text" }
    },
    {
        "type": "label",
        "id": "id_234"
    },
    {
        "id": "id_16677",
        "type": "div",
    },
    {
        "id": "id_155",
        "type": "input",
        "businesstype": { "type": "password" }
    }
]

var filterArray=[{type:'input',businesstype:{type:'text'}},{type:'div'}]
想要像这样的普通人

var output = [{
        "id": "id_1",
        "type": "input",
        "businesstype": { "type": "text" }
    },
    {
        "id": "id_16677",
        "type": "div",
    }
]

如何比较这两个对象以从元素中获得相等的对象。

可以使用递归方法对嵌套对象进行过滤

const isObject=o=>o&&typeof o==='object',
等质量=(f,o)=>
isObject(o)和Object.keys(f).every(k=>
等分对象(f[k])&&isEqual(f[k],o[k])|o[k]==f[k]
);
var元素=[{id:“id_1”,type:“input”,businesstype:{type:“text”},{type:“label”,id:“id_234”},{id:“id_16677”,type:“div”},{id:“id_155”,type:“input”,businesstype:{type:“password”}],
filterArray=[{type:'input',businesstype:{type:'text'}},{type:'div'}],
结果=elements.filter(o=>filterArray.some(f=>isEqual(f,o));
控制台日志(结果)

。作为控制台包装{max height:100%!important;top:0;}
您可以使用嵌套对象的递归方法对其进行筛选

const isObject=o=>o&&typeof o==='object',
等质量=(f,o)=>
isObject(o)和Object.keys(f).every(k=>
等分对象(f[k])&&isEqual(f[k],o[k])|o[k]==f[k]
);
var元素=[{id:“id_1”,type:“input”,businesstype:{type:“text”},{type:“label”,id:“id_234”},{id:“id_16677”,type:“div”},{id:“id_155”,type:“input”,businesstype:{type:“password”}],
filterArray=[{type:'input',businesstype:{type:'text'}},{type:'div'}],
结果=elements.filter(o=>filterArray.some(f=>isEqual(f,o));
控制台日志(结果)

.as控制台包装{max height:100%!important;top:0;}
如果您的
filterArray
在其层次结构中没有其他对象,您可以使用此解决方案-请参阅下面的演示:

var元素=[{id:“id_1”,type:“input”,businesstype:{type:“text”},{type:“label”,id:“id_234”},{id:“id_16677”,type:“div”},{id:“id_155”,type:“input”,businesstype:{type:“password”},[{type:“input”,businesstype:{type:“text”},{type:“div}];
var结果=元素。过滤器(函数(e){
返回filterArray.some(函数(f){
返回对象.keys(f).every(函数(k){
返回e.hasOwnProperty(k)和&Object.keys(f[k])。每个(函数(n){
返回e[k][n]==f[k][n];
});
});
});
});
控制台日志(结果)

.as控制台包装{top:0;最大高度:100%!important;}
如果您的
过滤器阵列
在其层次结构中没有其他对象,您可以使用此解决方案-请参阅下面的演示:

var元素=[{id:“id_1”,type:“input”,businesstype:{type:“text”},{type:“label”,id:“id_234”},{id:“id_16677”,type:“div”},{id:“id_155”,type:“input”,businesstype:{type:“password”},[{type:“input”,businesstype:{type:“text”},{type:“div}];
var结果=元素。过滤器(函数(e){
返回filterArray.some(函数(f){
返回对象.keys(f).every(函数(k){
返回e.hasOwnProperty(k)和&Object.keys(f[k])。每个(函数(n){
返回e[k][n]==f[k][n];
});
});
});
});
控制台日志(结果)
.as控制台包装{top:0;最大高度:100%!important;}
(自从您标记了Ramda之后)

Ramda已经有很多有用的(对象)比较函数,您可以使用它们使过滤器更易于阅读。(即:和其他在引擎盖下使用它的功能,如)

例如,您可以编写:

const elements=[{id:"id_1",type:"input",businesstype:{type:"text"}},{type:"label",id:"id_234"},{id:"id_16677",type:"div"},{id:"id_155",type:"input",businesstype:{type:"password"}}];
const filterArray=[{type:'input',businesstype:{type:'text'}},{type:'div'}];

// Describes how to define "equality"
// i.e.: elements are equal if type and businesstype match
// e.g.: pick(["a", "b"], { a: 1, b: 2, c: 3}) -> { a: 1, b: 2}
const comparisonObjectFor = pick(["type", "businesstype"]);

// Compares an object's comparison representation to another object
const elEquals = compose(whereEq, comparisonObjectFor);

// Creates a filter method that searches an array
const inFilterArray = matchElements => el => any(elEquals(el), matchElements);

// Run the code on our data
filter(inFilterArray(filterArray), elements);
运行示例

我认为这不一定是最好的解决方案(就可重用性、可读性而言),但我建议您不要内联深层对象/数组比较方法,因为:

  • 你可能会不止一次地使用它们
  • 如果没有正确的名称和文档,它们很难理解/预测
  • 由于其复杂性,它们容易出现(小)错误
  • 换句话说:既然您已经标记了lodash和Ramda,我可以安全地建议您使用经过良好测试、使用良好的库来比较您的对象。

    (自从您标记了Ramda)

    Ramda已经有很多有用的(对象)比较函数,您可以使用它们使过滤器更易于阅读。(即:和其他在引擎盖下使用它的功能,如)

    例如,您可以编写:

    const elements=[{id:"id_1",type:"input",businesstype:{type:"text"}},{type:"label",id:"id_234"},{id:"id_16677",type:"div"},{id:"id_155",type:"input",businesstype:{type:"password"}}];
    const filterArray=[{type:'input',businesstype:{type:'text'}},{type:'div'}];
    
    // Describes how to define "equality"
    // i.e.: elements are equal if type and businesstype match
    // e.g.: pick(["a", "b"], { a: 1, b: 2, c: 3}) -> { a: 1, b: 2}
    const comparisonObjectFor = pick(["type", "businesstype"]);
    
    // Compares an object's comparison representation to another object
    const elEquals = compose(whereEq, comparisonObjectFor);
    
    // Creates a filter method that searches an array
    const inFilterArray = matchElements => el => any(elEquals(el), matchElements);
    
    // Run the code on our data
    filter(inFilterArray(filterArray), elements);
    
    运行示例

    我认为这不一定是最好的解决方案(就可重用性、可读性而言),但我建议您不要内联深层对象/数组比较方法,因为:

  • 你可能会不止一次地使用它们
  • 如果没有正确的名称和文档,它们很难理解/预测
  • 由于其复杂性,它们容易出现(小)错误
  • 换句话说:既然您已经标记了lodash和Ramda,我可以安全地建议您使用经过良好测试、使用良好的库来比较您的对象