2

I have this 2 array :

let filterdata = [
    {
        "value": "item",
        "text": "additional_image_link",
        "custom": null,
        "updates": true,
        "removes": true
    },
    {
        "value": "url",
        "text": "adwords_grouping",
        "custom": null,
        "updates": true,
        "removes": true
    },
    {
        "value": "campaign_id",
        "text": "id",
        "custom": null,
        "updates": true,
        "removes": true
    }
];

let newdata = [
    {
        "value": "item",
        "text": "do_not_import",
        "custom": null,
        "updates": true,
        "removes": true
    },
    {
        "value": "url",
        "text": "do_not_import",
        "custom": null,
        "updates": true,
        "removes": true
    },
    {
        "value": "campaign_id",
        "text": "do_not_import",
        "custom": null,
        "updates": true,
        "removes": true
    },
    {
        "value": "campaign_url",
        "text": "do_not_import",
        "custom": null,
        "updates": true,
        "removes": true
    },
    {
        "value": "luck",
        "text": "do_not_import",
        "custom": null,
        "updates": true,
        "removes": true
    },
    {
        "value": "okay",
        "text": "do_not_import",
        "custom": null,
        "updates": true,
        "removes": true
    },
    {
        "value": "nope",
        "text": "do_not_import",
        "custom": null,
        "updates": true,
        "removes": true
    },
    {
        "value": "brand",
        "text": "do_not_import",
        "custom": null,
        "updates": true,
        "removes": true
    }
]

I want to marged this 2 array and the output should be like that:

[
    {
        "value": "item",
        "text": "additional_image_link",
        "custom": null,
        "updates": true,
        "removes": true
    },
    {
        "value": "url",
        "text": "adwords_grouping",
        "custom": null,
        "updates": true,
        "removes": true
    },
    {
        "value": "campaign_id",
        "text": "id",
        "custom": null,
        "updates": true,
        "removes": true
    },
    {
        "value": "campaign_url",
        "text": "do_not_import",
        "custom": null,
        "updates": true,
        "removes": true
    },
    {
        "value": "luck",
        "text": "do_not_import",
        "custom": null,
        "updates": true,
        "removes": true
    },
    {
        "value": "okay",
        "text": "do_not_import",
        "custom": null,
        "updates": true,
        "removes": true
    },
    {
        "value": "nope",
        "text": "do_not_import",
        "custom": null,
        "updates": true,
        "removes": true
    },
    {
        "value": "brand",
        "text": "do_not_import",
        "custom": null,
        "updates": true,
        "removes": true
    }
]

I am trying this :

let final = newdata.filter( ( item, index ) => {
    return item.text === 'do_not_import' && filterdata.hasOwnProperty( 'text' ) !== 'do_not_import' ;
});

console.log( final )

but no luck :(

Merged Logic:

You can see that filterdata value key is exist in newdata but in newdata text value is do_not_import and in filterdata text value is not do_not_import.

so in that case, its should remove the those filterdata from the newdata.

Shibbir
  • 1,963
  • 2
  • 25
  • 48
  • 1
    Can you explain the merging logic? Are you trying to merge two arrays together, or trying to merge the objects within the arrays together (if so, is there some key that determines if two objects merge?) – Nick Parsons Dec 17 '22 at 06:29
  • @NickParsons I have updated my question and I have written my best to explanation. Please have a look. – Shibbir Dec 17 '22 at 06:33
  • Also, can you explain why you're using `filter`, which is used to filter an array for elements that don't match your criteria, and can't generate new kinds of data, only "keep what you already have, or throw things away"? – Mike 'Pomax' Kamermans Dec 17 '22 at 06:40
  • @Mike'Pomax'Kamermans I have no idea how to get the result and I was trying. – Shibbir Dec 17 '22 at 06:42
  • There's a lot of questions on SO, if you google for "Javascript merge two arrays of objects on key" you get _so_ many results, including loads of Stackoverflow answers =) – Mike 'Pomax' Kamermans Dec 17 '22 at 06:44

1 Answers1

1

Check the below working snippet

let filterdata = [
    {
        "value": "item",
        "text": "additional_image_link",
        "custom": null,
        "updates": true,
        "removes": true
    },
    {
        "value": "url",
        "text": "adwords_grouping",
        "custom": null,
        "updates": true,
        "removes": true
    },
    {
        "value": "campaign_id",
        "text": "id",
        "custom": null,
        "updates": true,
        "removes": true
    }
];

let newdata = [
    {
        "value": "item",
        "text": "do_not_import",
        "custom": null,
        "updates": true,
        "removes": true
    },
    {
        "value": "url",
        "text": "do_not_import",
        "custom": null,
        "updates": true,
        "removes": true
    },
    {
        "value": "campaign_id",
        "text": "do_not_import",
        "custom": null,
        "updates": true,
        "removes": true
    },
    {
        "value": "campaign_url",
        "text": "do_not_import",
        "custom": null,
        "updates": true,
        "removes": true
    },
    {
        "value": "luck",
        "text": "do_not_import",
        "custom": null,
        "updates": true,
        "removes": true
    },
    {
        "value": "okay",
        "text": "do_not_import",
        "custom": null,
        "updates": true,
        "removes": true
    },
    {
        "value": "nope",
        "text": "do_not_import",
        "custom": null,
        "updates": true,
        "removes": true
    },
    {
        "value": "brand",
        "text": "do_not_import",
        "custom": null,
        "updates": true,
        "removes": true
    }
]

let combinedArr = [...filterdata, ...newdata ];
let result = Array.from(new Set(combinedArr.map(a => (a.value))))
.map(val => { return combinedArr.find(item => (item.value === val)); });
console.log(result)
Amaarockz
  • 4,348
  • 2
  • 9
  • 27