[英]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.