1

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'];

2 Answers 2

2

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)
Sign up to request clarification or add additional context in comments.

Comments

2

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; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.