-1

I have this array of object in JavaScript :

let arrOfObject = [ { "value": "title", "text": "additional_product_type", "custom": null }, { "value": "short_message", "text": "adwords_grouping", "custom": null }, { "value": "url", "text": "adwords_labels", "custom": null }, { "value": "campaign_id", "text": "age_group", "custom": null }, { "value": "aff_code", "text": "Do Not Import", "custom": null }, { "value": "campaign_url", "text": "Do Not Import", "custom": null }, { "value": "other_data", "text": "Do Not Import", "custom": null }, { "value": "product_id", "text": "Do Not Import", "custom": null }, { "value": "created_at", "text": "Do Not Import", "custom": null }, { "value": "product_active", "text": "Do Not Import", "custom": null }, { "value": "brand", "text": "Do Not Import", "custom": null }, { "value": "old_price", "text": "Do Not Import", "custom": null }, { "value": "subcategory", "text": "Do Not Import", "custom": null }, { "value": "category", "text": "Do Not Import", "custom": null }, { "value": "price", "text": "Do Not Import", "custom": null }, { "value": "widget_name", "text": "Do Not Import", "custom": null }, { "value": "campaign_name", "text": "Do Not Import", "custom": null }, { "value": "image_urls", "text": "Do Not Import", "custom": null }, { "value": "description", "text": "Do Not Import", "custom": null } ]  ;

Now, I want to filter through this array and remove those object which object text value is Do Not Import and store that filtered array to this original array.

But I can't change the original array. I need to use another array to get the final filtered array. Is there any other way?

What I am doing is:

let newArray = [];

arrOfObject.map( ( item, index ) => {
    if( item.text !== 'Do Not Import') {
        newArray.push(item);
    }
})

console.log( newArray )
Shibbir
  • 1,963
  • 2
  • 25
  • 48
  • Are you asking how to filter an array? Did you google for that? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter – Andy Ray Aug 15 '22 at 04:45

1 Answers1

2

Just use filter

arrOfObject = arrOfObject.filter( ( item, index ) => {
    return item.text !== 'Do Not Import';
})

Mohamed Abdallah
  • 796
  • 6
  • 20