简体   繁体   English

比较两个 arrays 的对象并根据一个对象数组排序并在不匹配时推送

[英]compare two arrays of objects and sort based on one array of objects and push when doesn't match

I have two arrays in javascript我在javascript中有两个arrays

array1 = [{id: 4, value:'xyz'}, {id: 2, value:'pqr'}, {id: 5, value:'abc'}]
array2 = [
         [{id: 5, value:'abc'}, {id: 4, value:'xyz'}],
         [{id: 5, value:'abc'}, {id: 2, value:'pqr'}],
         [{id: 4, value:'xyz'}, {id: 2, value:'pqr'}],
         [{id: 2, value:'pqr'}, {id: 5, value:'abc'}]
]

array2 is array of array of objects, all these arrays inside array 2 should be first of all sorted as per the array1 sequence and then object with null should be pushed if that particular id doesn't exist at that particular index. array2 是对象数组的数组,数组 2 中的所有这些 arrays 应首先按照 array1 序列排序,然后如果该特定索引处不存在该特定 ID,则应推送 object 和 null。

output = [
         [{id: 4, value:'xyz'}, {id: null, value:null}, {id: 5, value:'abc'}],
         [{id: null, value:null}, {id: 2, value:'pqr'}, {id: 5, value:'abc'}],
         [{id: 4, value:'xyz'}, {id: 2, value:'pqr'}, {id: null, value:null}],
         [{id: 4, value:'xyz'}, {id: 2, value:'pqr'}, {id: null, value:null}],
         [{id: null, value:null}, {id: 2, value:'pqr'}, {id: 5, value:'abc'}]
].

Here is my code这是我的代码

export const mapOrder = (array, order, key) => {
    let arr = [];
    array.sort((a, b) => {
        let A = a[key];
        let B = b[key];
        if (order.indexOf(A) > order.indexOf(B)) {
            return 1;
        }
        return -1;
    });
    if (order.length > 0) {
        for (let i = 0; i < order.length; i++) {
            for (let j = 0; j < array.length; j++) {
                if (order[i] === array[j].UserId) {
                    arr.push(array[j]);
                } else {
                    arr.push({ id: null, value: null})
                }
            }
        }
        array = arr;
    }
    return arr;
};

First you can loop over the second array and save the ID's to a new Set, Then loop over the first array and if the current item's ID is present in the set then return the current object or return the object with id and value as null`首先,您可以遍历第二个数组并将 ID 保存到一个新的 Set,然后遍历第一个数组,如果集合中存在当前项目的 ID,则返回当前的 object 或返回id和值为 null 的 object

 const array1 = [{id: 4, value:'xyz'}, {id: 2, value:'pqr'}, {id: 5, value:'abc'}] const array2 = [{id: 5, value:'abc'}, {id: 4, value:'xyz'}] function test(arr1, arr2) { const set = new Set() arr2.forEach(i => set.add(i.id)) return arr1.map((i) => set.has(i.id)? i: { id: null, value: null }) } console.log(test(array1,array2))

You could make a Map from your array2, where each id from the objects in array2 point to an array of objects with that particular id value.您可以从 array2 中创建一个 Map,其中 array2 中对象的每个 id 都指向具有该特定id值的对象数组。 Then you can use .flatMap() on array1 and get all the objects for the id of the currently iterated object by using .get() on your map. If .get() returns nothing, you can provide the default object (where id and value are null ):然后您可以在array1上使用.flatMap()并通过在您的 map 上使用.get()获取当前迭代 object 的id的所有对象。如果.get()不返回任何内容,您可以提供默认的 object(其中 id和值是null ):

 const array1 = [{id: 4, value:'xyz'}, {id: 2, value:'pqr'}, {id: 5, value:'abc'}]; const array2 = [{id: 5, value:'abc'}, {id: 4, value:'xyz'}]; // group the arrays into a Map const grouped = array2.reduce((map, obj) => map.set(obj.id, (map.get(obj.id) || []).concat(obj)), new Map ); const res = array1.flatMap(({id}) => grouped.get(id) || ({id: null, value: null})); console.log(res);

You might be able to remove the .flatMap() and the need to array of objects in your Map if objects in array2 have unique id values (ie: an object with the same idea cannot exist).如果 array2 中的对象具有唯一的id值(即:具有相同想法的 object 不存在),您可能能够删除.flatMap()Map中对象数组的需要。 If this can be the case, you can simplify the above:如果是这种情况,您可以简化以上内容:

 const array1 = [{id: 4, value:'xyz'}, {id: 2, value:'pqr'}, {id: 5, value:'abc'}]; const array2 = [{id: 5, value:'abc'}, {id: 4, value:'xyz'}]; const ids = new Map(array2.map((obj) => [obj.id, obj])); const res = array1.map(({id}) => ids.get(id) || ({id: null, value: null})); console.log(res);

You could collect the object in an object first and them map the collected objects or take a placeholder.您可以先在 object 中收集 object,然后将 map 收集到的对象或占位符。

 var array1 = [{ id: 4, value: 'xyz' }, { id: 2, value: 'pqr' }, { id: 5, value: 'abc' }], array2 = [{ id: 5, value: 'abc' }, { id: 4, value: 'xyz' }], result = array1.map( (o => ({ id }) => o[id] || { id: null, value: null }) (array2.reduce((r, o) => ({...r, [o.id]: o }), {})) ); console.log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

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

相关问题 jQuery比较两个数组对象以拥有一个最终的数组对象 - jQuery compare two arrays objects to have one final array objects 比较两个数组中的对象并根据javascript中的匹配返回 - Compare objects in two arrays and return based on match in javascript Ramda 根据一个属性比较两个 arrays 的对象 - Ramda compare two arrays of objects based on one property 两个对象数组,需要根据另一个对象对它们进行排序 - Two arrays of objects and need to sort one based on the other 试图比较两个对象的 arrays 并从一个数组中删除匹配的元素并返回它,目前只删除第一个匹配项 - Trying to compare two arrays of objects and remove matching elements from one array and return it, currently only removing first match 比较对象的 arrays 并返回不在 arrays 之一中的新对象数组 - Compare arrays of objects and return new array of objects that are not in one of the arrays Javascript:基于时间戳属性对对象数组进行排序不会正确排序一个元素 - Javascript: Sorting an array of objects based on a timestamp property doesn't sort one element correctly 比较两个对象数组 - compare two arrays of objects 比较两个对象数组 - Compare two arrays of objects 比较两个对象数组,如果匹配则返回 true - Compare two array of objects and return true if they are match
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM