Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I change the value of the array in the object in the array?

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>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading