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"},
...,
...
]
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 1
    Array of objects is not diffrent from another array, iterate it with for or foreach loop and then you can iterate through the object key and value using Object.entries() (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries).. and of course add to new array only the objects with the keys and values that you need. – ValeriF21 Dec 24 '20 at 22:57
  • Does this answer your question? [Remove property for all objects in array](https://stackoverflow.com/questions/18133635/remove-property-for-all-objects-in-array) – pilchard Dec 24 '20 at 23:18

2 Answers2

3

You could go over them and delete those keys:

array.forEach(o => delete o.message); 
Mureinik
  • 297,002
  • 52
  • 306
  • 350
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
    }
}
Eduardo Poço
  • 2,819
  • 1
  • 19
  • 27