Is there a way to remove objects from a JSON array that are not included in another array?
let originalArray = [{
id: 1,
NoOfEmp: 12,
Wages:1000,
TotalSI: 21,
Salary:3000
}, {
id: 2,
NoOfEmp: 13,
Wages:2000,
TotalSI: 22,
Salary:4000
}]
let keepArrayObjects = ['id','NoOfEmp','Wages']
originalArray = originalArray.filter( function( el ) {
return keepArrayObjects.indexOf( el ) > 0;
} );
console.log(originalArray);
The code above was an attempt to remove the "TotalSI" and "Salary" objects, while keeping those listed in the keepArrayObjects array. However, it did not work as expected.
I am looking to modify the original array itself. Can someone provide assistance with this?