When I press the button, I want to nullify the values of the items in the itemSold and itemGet in the customers. How do I do this?
What should be done?
1- I have to press the button
2- When the button is pressed, the values inside should be null
I want to do this :
let customers = [{
active: true,
id: 1,
product: {
itemSold: [null, null],
itemGet: [null, null],
},
},
{
active: true,
id: 2,
product: {
itemSold: [null, null],
itemGet: [null, null],
},
},
];
let customers = [{
active: true,
id: 1,
product: {
itemSold: [{id:1,name : 'car'}, {id:2,name : 'home'}],
itemGet: [{id:3,name : 'phone'}, {id:4,name : 'fly'}],
},
},
{
active: true,
id: 2,
product: {
itemSold: [{id:5,name : 'lamb'}, {id:6,name : 'mouse'}],
itemGet: [{id:7,name : 'mouse pad'}, {id:8,name : 'tv'}],
},
},
];
let clickButton = document.querySelector("#clickButton");
let products = [];
clickButton.addEventListener("click", removeProducts()});
function removeProducts(){}
<button id="clickButton" >Click
</button>
>Solution :
You can use Array.map() to create a new array populated with the results of calling a provided function on every element in the calling array.
let customers=[{active:!0,id:1,product:{itemSold:[{id:1,name:"car"},{id:2,name:"home"}],itemGet:[{id:3,name:"phone"},{id:4,name:"fly"}]}},{active:!0,id:2,product:{itemSold:[{id:5,name:"lamb"},{id:6,name:"mouse"}],itemGet:[{id:7,name:"mouse pad"},{id:8,name:"tv"}]}}];
document.querySelector("#clickButton").addEventListener("click", removeProducts);
function removeProducts() {
customers = customers.map(customer => ({
...customer,
product: {
itemSold: customer.product.itemSold.map(i => null),
itemGet: customer.product.itemGet.map(i => null)
}
}))
console.log(customers)
}
<button id="clickButton">Click
</button>