Reputation: 415
How to remove data from another data array that already existed.
for example:
data = [{
name: "james",
device: "device1"
}, {
name: "justine",
device: "device2"
}];
device = [
'device1', 'device2', 'device3', 'device4', 'device5'
];
how to remove the device that already exist?
expected output:
device = ['device3', 'device4', 'device5'];
Upvotes: 1
Views: 38
Reputation: 73906
You can try this:
const data = [{name:"james",device:"device1"},{name:"justine",device:"device2"}];
let device = ["device1","device2","device3","device4","device5"];
// Get all the devices to be removed first
var remove = data.map(({device}) => device)
// Delete the above devices which are present in device array
// using filter() method
device = device.filter(d => !remove.includes(d));
console.log(device)
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 2
Reputation: 7574
First, get an array of the "existing" devices from your data structure:
const usedDevices = data.map((el) => el.device)
Then, filter all devices by checking if they are in your usedDevices
list
const remaining = device.filter((el) => usedDevices.indexOf(el) < 0)
There you go:
console.log(remaining)
Upvotes: 2