How to use the rest syntax with object destructuring In Javascript
In this method, we will use rest syntax in object destructuring to get all the properties except that property that we don’t want. We can achieve this using a rest combination with a map.
Example: This example shows the use of the above-explained approach.
Javascript
const carResponse = [{ name: "Alto" , brand: "Maruti suzuki" , model: 800, color: "black" }, { name: "Fortuner " , brand: "Toyota" , model: 789, color: "white" }, { name: "Thar" , brand: "Mahindra" , model: 249, color: "red" }, { name: "Kwid" , brand: "Renault" , model: 346, color: "yellow" } ] const output = carResponse.map(({ model, ...rest }) => rest) console.log(output) |
Output
[
{ name: 'Alto', brand: 'Maruti suzuki', color: 'black' },
{ name: 'Fortuner ', brand: 'Toyota', color: 'white' },
{ name: 'Thar', brand: 'Mahindra', color: 'red' },
{ name: 'Kwid', brand: 'Renault', color: 'yellow' }
]
JavaScript Program to Delete Property from Spread Operator
This article will demonstrate how to delete property from the spread operator in JavaScript. In JavaScript, the spread operator allows to create a shallow copy of an objеct or mеrgе propеrtiеs from one objеct into another without modifying the original objеct.
Table of Content
- Using JavaScript object destructuring
- Using the rest syntax with object destructuring
Contact Us