There are three tasks in the question:
- Check the value for primitiveness
- Create a new object based on the old, without some keys.
- Build a new array based on the old one.
To solve the first one, you need to decide what to consider as primitive types: if only strings and numbers are one condition, if in addition to them there is also null , undefined , function is another.
For example, take only strings and numbers, then the test can be made into such a function isPrimitive
function isPrimitive(o){ return typeof o == 'string' || typeof o == 'number'; }
To solve the second, you can use the reduce function: by applying it to an array of keys, you can collapse it into an object without unnecessary properties, for example, weaving:
Object.keys(o).reduce((acc,key)=>{ if(isPrimitive(o[key])) acc[key] = o[key]; return acc; },{});
To solve the third one, use the map function.
Assembly may look like this:
var array = [{ name: 'Ivan', definition: { head: true } }, { name: 'Fedor', definition: { head: false } }]; function isPrimitive(o) { return typeof o == 'string' || typeof o == 'number'; } var newArray = array.map(o => Object.keys(o).reduce((acc, key) => { if (isPrimitive(o[key])) acc[key] = o[key]; return acc; }, {})); console.log(newArray);