i have array of objects that can be removed with another array of objects ie
data = [
{"id": 1, name: "a", qty: 1},
{"id": 2, name: "b", qty: 1},
{"id": 2, name: "b", qty: 1, bonusFromId: 3},
{"id": 2, name: "b", qty: 1, bonusFromId: 1},
{"id": 3, name: "c", qty: 1}
]
and i want to splice the array where the ids are same with the bonusFromId
temp = [
{"id": 1, name: "a", qty: 1},
{"id": 3, name: "b", qty: 1}
]
the value that i expect is
[
{"id": 1, name: "a", qty: 1}
{"id": 2, name: "b", qty: 1}
{"id": 3, name: "c", qty: 1}
]
i tried with this but it didnt worked
data.forEach(x => {
let index = temp.filter(y => x.bonusFromId == y.id).map((item) => { return item.id}).indexOf(x.id)
arr.splice(index, 1)
})
const data = [ {"id": 1, name: "a", qty: 1}, {"id": 2, name: "b", qty: 1}, {"id": 2, name: "b", qty: 1, bonusFromId: 3}, {"id": 2, name: "b", qty: 1, bonusFromId: 1}, {"id": 3, name: "c", qty: 1} ] const temp = [ {"id": 1, name: "a", qty: 1}, {"id": 3, name: "b", qty: 1} ] temp.forEach(t=>{ let find = data.find(e => e.bonusFromId == t.id) // find t.id in data.bonusFromId let index = data.indexOf(find); // get index of item in array data data.splice(index, 1); // splice it console.log(index) }) console.log(data)
.as-console-wrapper{ min-height: 100%}
Or as stated in comment:
const data = [ {"id": 1, name: "a", qty: 1}, {"id": 2, name: "b", qty: 1}, {"id": 2, name: "b", qty: 1, bonusFromId: 3}, {"id": 2, name: "b", qty: 1, bonusFromId: 1}, {"id": 3, name: "c", qty: 1} ] const temp = [ {"id": 1, name: "a", qty: 1}, {"id": 3, name: "b", qty: 1} ] temp.forEach(t=>{ let find = data.findIndex(e => e.bonusFromId == t.id) find >= 0? data.splice(find, 1): null console.log(find) }) console.log(data)
.as-console-wrapper{ min-height: 100%}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.