Tips for breaking down an item and removing a specific attribute?

I am attempting to break down an object and remove a specific property from it.

Here is the current approach I am taking:

 let x = {a:1, b:2, c:3};
 let y = {...x};
 delete y.c;
 store.setState(y);

I came across an article discussing ES7 that introduced a new method for excluding properties. The revised code would look like this:

 let x = {a:1, b:2, c:3};
 store.setState({c, ...x});

https://codeburst.io/use-es2015-object-rest-operator-to-omit-properties-38a3ecffe90

Unfortunately, the above technique does not function properly in Angular 7, resulting in the following error message:

error TS2663: Cannot find name 'c'. Did you mean the instance member 'this.c'?

I am currently using TypeScript 3.1.6, and my tsconfig.app.json file appears as follows.

{
    "extends": "../tsconfig.json",
    "compilerOptions": {
        "outDir": "../out-tsc/app",
        "module": "es2015",
        "types": []
    },
    "exclude": [
        "src/test.ts",
        "**/*.spec.ts"
    ]
}

Below is the parent file.

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "importHelpers": true,
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}

It seems that specifying "lib": ["es2017"] pertains to ES6, which may be causing the issue.

How can I enable this ES7 feature without affecting the target ES5 output from TypeScript?

Answer №1

To quickly make the change, replace let y = {c, ...x}; with const {c, ...noC} = x;. Your updated code should look like this -

const x = {a:1, b:2, c:3};
const {c, ...noC} = x;

console.log(noC);

Tip: It's always a good idea to declare variables as constants like x if you don't plan on changing their values later.

Answer №2

It is important that the sequence of orders follows this format:

const { a, ...b } = x;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can the return type of a function that uses implicit return type resolution in Typescript be displayed using "console.log"?

When examining a function, such as the one below, my curiosity drives me to discover its return type for educational purposes. function exampleFunction(x:number){ if(x < 10){ return x; } return null } ...

Unable to access a user's public information using Instagram's API

I've spent the past week trying to create a simple Instagram preview application that should show a user's public data such as username, followers, following, and profile picture URL, but unfortunately, I haven't been able to find a solution ...

Is there a method by which I can access information from a linked document in Firebase?

I am relatively new to firebase and I am attempting to retrieve data from a referenced document in another collection to display. Link 1 Link 2 This is how I add a student and the parent's ID: const newStudent = { name: req.body.name, grade: ...

There was a problem finding the correct address indicated by the marker

I am working on an Android app using PhoneGap, and I need to display a marker on a Google map at a specific latitude and longitude. When the marker is clicked, I want to show an info window displaying the address associated with that location. However, t ...

Differences between .keyCode and .whichExplanation of the

I was under the impression that I could find an answer to this on Stack Overflow, but it seems elusive. When listening for a keypress event, is it better to use .keyCode or .which to check if the Enter key was pressed? Typically, I've used something ...

Node - Elasticsearch index initialized without any documents

Currently, I have been developing an AWS Lambda function that is triggered by notifications from an S3 Bucket whenever it receives logs from Cloudfront. I have successfully implemented the functionality to decompress the log packages and parse them using t ...

Inform users once the new application build using Angular 2 with Angular CLI has been deployed

Currently using the angular cli, I am looking for a way to alert users once a new version of the app has been deployed. My current approach involves utilizing replace-in-file and an external JavaScript file that is executed prior to building the applic ...

Error: There was an issue registering the component as the target container is not recognized as a valid DOM element

Upon executing the React code below, I encountered the following error: import React from 'react'; import ReactDOM from 'react-dom'; ReactDOM.render( <div id="root"> <h1>Hello, world!</h1></div>, document ...

Assurance-driven number tally

I'm diving into JavaScript and recently started exploring promises. I've put together a code snippet that logs the value passed to the promise function as a parameter after the setTimeout function is triggered. Now, I'm wondering if there&ap ...

Utilizing Webpack for Effortless Image Loading in Angular HTML

I've been struggling with webpack and angular configuration. It seems like a simple issue, but I just can't seem to find the solution. Despite scouring Stack Overflow for answers, I'm still stuck. My HTML page looks like this (along with ot ...

ForEach fails to refresh the primary array

I am currently working on extending the values of each item in an array. My array consists of objects with x, y, and z fields. I aim to append more items to each object within the array using information obtained from a http.get call's response. The ...

Once setTimeout runs for the first time, it does not work again

I have a piece of JavaScript code that I want to make all Bootstrap alerts disappear after 1 second. It seems to work the first time, but if I add more alerts they remain on the screen. What could be causing this issue? $(document).ready(function () { func ...

Image can be centered, but unable to center div in IE7

When trying to center an element both vertically and horizontally, everything seems to be working correctly except for one issue I'm facing in IE7. I am able to center an img vertically but not a div. What style is being applied by IE to the image tha ...

Transferring data between functional components in ReactJS and dealing with the output of [object Object] or undefined

I'm struggling with passing a prop in functional components that are both located in the same .js file. I previously attempted to seek help for this issue, but unfortunately, it wasn't productive. My goal is to extract the member_id from the GET ...

Differentiating TypeScript classes is important in situations where they would normally be treated as equivalent

Looking to add a lightweight layer on top of Float32Array to create vec2 and vec3's. Check out the example below: class vec3 extends Float32Array { // w : number; constructor() { super(3); // this.w = 1; } static add3(x: ...

Linking JavaScript on the client side to a NodeJS application on the server side

I am new to NodeJS and I am currently exploring the app structure. I have developed a basic app using Socket.IO and MongoJS, which functions as a tracking system that gathers variables from a client-side script and stores them in Mongo. This is how I envi ...

Implementing manual updates for the bootstrap color picker

I have integrated a color picker from Bootstrap into my project. You can find the color picker here: To change the background color of an element on my page, I am using the following code: <input type="text" id="mypicker" name="mypicker" value="" cla ...

What is the alternative method of sending a POST request instead of using PUT or DELETE in Ember?

Is there a way to update or delete a record using the POST verb in Ember RESTAdapter? The default behavior is to send json using PUT or DELETE verbs, but those are blocked where I work. I was wondering if there's a way to mimic Rails behavior by send ...

Retrieving data sent through an AJAX post request

My current project involves making a POST call from a basic HTML page to a Node.js and Express server that will then save the input values to a MongoDB collection. The issue I am facing is that when passing two POST parameters, namely 'name' and ...

Run the *.js file only when the current month is December

Alright, I'm stumped. I've been trying to create this script: <script> $(document).ready(function(){ var d = new Date(); n = d.getMonth(); if (n == 11) { src="extrafiles/effect/snow.js"; } }); </script& ...