Hope this is what you are looking for !!
const Fruits = [
{ name: "Apple", code: "1" },
{ name: "Orange", code: "2" },
{ name: "Mango", code: "3" },
{ name: "Derry", code: "4" }
];
const fruitsSel = document.querySelector('#fruits');
Fruits.forEach(function(item, index, array) {
var opt = document.createElement("option");
opt.text = item.name;
opt.value = item.code;
fruitsSel.add(opt);
});
const SelectedFruits=[];
const remainingFruits = [];
fruitsSel.addEventListener('change',(e)=>{
const val = e.target.value;
const selectedFruit = Fruits.filter(fruit=>fruit.code===val);
SelectedFruits.push(...selectedFruit);
const remFruits = Fruits.filter(fruit=>fruit.code!==val);
remainingFruits.push(...remFruits);
console.log({'selected fruit' : selectedFruit})
console.log({'remaining fruits' : remFruits})
})
<select id='fruits'>
</select>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…