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.

5
  • 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?) Commented Dec 17, 2022 at 6:29
  • @NickParsons I have updated my question and I have written my best to explanation. Please have a look. Commented Dec 17, 2022 at 6: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"? Commented Dec 17, 2022 at 6:40
  • @Mike'Pomax'Kamermans I have no idea how to get the result and I was trying. Commented Dec 17, 2022 at 6: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 =) Commented Dec 17, 2022 at 6:44

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. Its exactly what I want but can it be more simplyfied?
Yes, probably will post it in some time. Meanwhile now that you have this solution you can also try to optimize it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.