Trying to return a new array of objects that have the name and index from the original array. The solution should be as follows:
[{name: 'Jane', index: 1}, {name: 'mark', index: 3}, {name: 'sophia', index: 4}]
--
let array = [
{a: 2},
{name: "jane"},
{},
{name: 'mark'},
{name: 'sophia'},
{b: 1}
]
function somefunction (somearr) {
newarray = [];
somearr.forEach(element, index => {
if (element.name != null) {
let newobj = {}
newobject.name = element.name
newobject.index = index
newarray.push(newobj)
}
})
console.log(newarray)
}
somefunction(array)
I am getting the following error:
Process exited with code 1
Uncaught ReferenceError ReferenceError: element is not defined