我有一个对象变量的old_data
数组:
let old_data = [
{
value: 'item',
text: "do_not_import",
custom: null,
updates : true,
removes : true,
},
{
value: 'additional_image',
text: "additional_image_mapped",
custom: null,
updates : true,
removes : true,
},
{
value: 'brand',
text: "brand_mapped",
custom: null,
updates : true,
removes : true,
},
];
现在,我正在过滤这个变量,其中text不是do_not_import
let filter_data = old_data.filter( ( item ) => {
return item.text != 'do_not_import';
});
现在,我得到了这个结果:
[
{
value: 'additional_image',
text: 'additional_image_mapped',
custom: null,
updates: true,
removes: true
},
{
value: 'brand',
text: 'brand_mapped',
custom: null,
updates: true,
removes: true
}
]
好的。现在我有new_data
可变:
let newData = [
{
value: 'item',
text: "do_not_import",
custom: null,
updates : true,
removes : true,
},
{
value: 'additional_image',
text: "do_not_import",
custom: null,
updates : true,
removes : true,
},
{
value: 'brand',
text: "do_not_import",
custom: null,
updates : true,
removes : true,
},
{
value: 'new',
text: "do_not_import",
custom: null,
updates : true,
removes : true,
},
];
现在,我想合并new_data
和filter_data
变量,它们应该返回来自filter_data
的所有数据和来自new_data
的所有数据,但从new_data
它应该返回那些关键字value
在filter_data
变量中不相同的数据。
这意味着我想要的输出是:
[
{
value: 'additional_image',
text: 'additional_image_mapped',
custom: null,
updates: true,
removes: true
},
{
value: 'brand',
text: 'brand_mapped',
custom: null,
updates: true,
removes: true
},
{
value: 'item',
text: "do_not_import",
custom: null,
updates : true,
removes : true,
},
{
value: 'new',
text: "do_not_import",
custom: null,
updates : true,
removes : true,
}
]
这应该完成任务-
let old_data = [
{
value: 'item',
text: "do_not_import",
custom: null,
updates : true,
removes : true,
},
{
value: 'additional_image',
text: "additional_image_mapped",
custom: null,
updates : true,
removes : true,
},
{
value: 'brand',
text: "brand_mapped",
custom: null,
updates : true,
removes : true,
},
];
let filter_data = old_data.filter( ( item ) => {
return item.text != 'do_not_import';
});
let newData = [
{
value: 'item',
text: "do_not_import",
custom: null,
updates : true,
removes : true,
},
{
value: 'additional_image',
text: "do_not_import",
custom: null,
updates : true,
removes : true,
},
{
value: 'brand',
text: "do_not_import",
custom: null,
updates : true,
removes : true,
},
{
value: 'new',
text: "do_not_import",
custom: null,
updates : true,
removes : true,
},
];
var values = new Set(filter_data.map(v => v.value));
var merged = [...filter_data, ...newData.filter(v => !values.has(v.value))];
console.log(merged)
这应该可以工作。
const combinedData=newData.reduce((acc,element)=>acc.some(e=>e.value===element.value)?acc:[…acc,element],filteredData);
const filterData = [
{
value: 'additional_image',
text: 'additional_image_mapped',
custom: null,
updates: true,
removes: true
},
{
value: 'brand',
text: 'brand_mapped',
custom: null,
updates: true,
removes: true
}
];
const newData = [
{
value: 'item',
text: "do_not_import",
custom: null,
updates: true,
removes: true,
},
{
value: 'additional_image',
text: "do_not_import",
custom: null,
updates: true,
removes: true,
},
{
value: 'brand',
text: "do_not_import",
custom: null,
updates: true,
removes: true,
},
{
value: 'new',
text: "do_not_import",
custom: null,
updates: true,
removes: true,
},
];
const consolidateArray = [];
for(const data of newData){
const getObjectFromFilterArray = filterData.find(x => x.value === data.value);
if(getObjectFromFilterArray){
consolidateArray.push(getObjectFromFilterArray);
}else{
consolidateArray.push(data);
}
}
console.log(consolidateArray);