JavaScript -过滤后合并2个对象

hfwmuf9z  于 2022-12-17  发布在  Java
关注(0)|答案(3)|浏览(158)

我有一个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_datafilter_data变量,该变量应返回filter_data中的所有数据和new_data中的所有数据,但对于new_data,它应返回那些键valuefilter_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,        
    }
]
yjghlzjz

yjghlzjz1#

这个应该可以-

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)
vtwuwzda

vtwuwzda2#

这应该行得通。
常量组合数据=新数据.减少((账户,元素)=〉账户.一些(e =〉e.值===元素.值)?账户:[... acc,元素],过滤数据);

slwdgvem

slwdgvem3#

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);

相关问题