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

1 Answer 1

2

Just use filter

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.