1

I have an array of objects I generated from a JSON response.

The content of said array is structured like this:

const array = [
{title: "user generated title", message: "random user generated text", status: "Active/Inactive"},
 {200 + more objects with the exact same key/values as the first},
...,
...
]

I want to make a new array with the exact same objects minus specific key/value pairs.

I.e., to make the exact same array with out say all message: "user message" key/value pairs. (There are multiple key/value pairs I want to remove from the objects in the array though.)

so it would be like

const array = [
{title: "user generated title", status: "Active/Inactive"},
{200 + objects just like the first without the message: "message text"},
...,
...
]
2

2 Answers 2

3

You could go over them and delete those keys:

array.forEach(o => delete o.message); 
Sign up to request clarification or add additional context in comments.

Comments

1
var newArray = [];
for (var i = 0; i < array.length; i++) {
    var obj = array[i];
    
    if (!("message" in obj)) { //put the conditions to keep the object here
        newArray.push(obj); //copy reference to new array
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.