Questions tagged [object-destructuring]

Use this tag for JavaScripts object destructuring syntax.

JavaScript object destructuring allows one to replace code like:

const object = {foo: 'bar', baz: 'bing'};
const foo = object.foo;
const baz = object.baz;

With this destructuring assignment syntax:

const object = {foo: 'bar', baz: 'bing'};
const {foo, baz} = object;
155 questions
0
votes
1 answer

how to destruc an object which includes a long string with dot as the key

I have an object which includes string with dot "journey.hintText.journeyTags" as the key. (returned by api response i cannot modify). I would like to destruct content object like const {journey.hintText.journeyTags} = content, but it throws error.…
Yang Wang
  • 580
  • 4
  • 14
0
votes
0 answers

Object Destructuring not working in a ReactJs script

I am using Next js for an application. The code components have a global state stored in parent and passed down to child components using ReactContext. In one of the components, when I am trying to create a state component by destructuring a parent…
user3445623
  • 3
  • 1
  • 5
0
votes
1 answer

Can you use js object destructuring but modify some of the values before assigning to the new object?

I have the following code that receives movie data from an api. Now I want to create a new object from the returned json file but only containing a subset of the properties while also modifying some of the existing property values before assigning…
0
votes
0 answers

Typing const from rest param in destructured object

I'm trying to explicitly type a const which is created using a rest param in a destructured object. I can see that typescript can infer that it will have all the properties of the type of the source object, less any properties which I create…
sauntimo
  • 1,531
  • 1
  • 17
  • 28
0
votes
1 answer

How to have typescript determine non-safe destructuring scenarios?

Example code below would lead to a destructuring error because it'd try to destructure undefined. How can we prevent typescript from compiling successfully for this kind of unsafe destructuring code? interface IOptionalObjectProperties { prop?: { …
Meet
  • 294
  • 3
  • 8
0
votes
2 answers

Javascript - Creating a universal function that can take destructured object properties as arguments but where some properties might be missing

I want to find a way how to pass different objects to one same function and let the function process only inputs that are present in the given object. Specifically I am passing different Angular Components as this and am destructuring the object…
0
votes
0 answers

How to completely destructure object Javascript

If I had a large object, and needed all the properties of that object, is there a quick way to destructure that object? So this: const foo = { name1: 'foo', num1: 5, bool1: true friends1: ['bar', 'fizz', 'buzz'], name2: 'foo', num2: 5, …
0
votes
1 answer

ES6 destructure and spread together as arguments into a React Component

I understand ES6 destructure and spread operators, but I am confused by their usage together. Can someone break this down into a way a layman can understand? const index = ({title, description, ...props}) => { return (
AJSwift
  • 709
  • 4
  • 12
  • 26
0
votes
2 answers

how is it possible that we can destructe an object using any property name in React?

I have a doubt can any body help me with my doubt or can any one tell me where I can find my answer? So as we know in object destructing we must use the same property name to destructure. However, in React when we destructure useParams from…
0
votes
1 answer

Is it possible to use type assertion/cast inside an object destructuring expression?

Instead of writing this in Typescript: const ADMIN_PRODUCT = useAdminProduct(); const mode = ADMIN_PRODUCT.mode; const product = ADMIN_PRODUCT.product as TYPES.PRODUCT; // I NEED TO ASSERT: product as TYPES.PRODUCT const invalidFields =…
0
votes
2 answers

Function parameter value isn't working in object-destructuring in JS

Let's assume that I've the following object: let filters = { brands: { ... }, price: { ... }, sizes: { ... }, ... } The properties of the filters object will be set by the users. Which means sometimes the filters object may contain just…
snsakib
  • 1,062
  • 9
  • 14
0
votes
0 answers

Shorten two parameters using default values from the properties of the same object using destructuring

Can the following function (which is inside a class) be shortened: getCurrentMoves(moves = this.state.moves, stepNumber = this.state.stepNumber) { return moves.slice(0, stepNumber); } All I managed to do is to refactor it to the following.…
0
votes
2 answers

How can I handle change in multiple input field and send the data to an api with axios?

I'm trying to handle change in multiple input text field and send the value to an API Here is what i have tried const [state, setState] = useState({ Value_A:'', Value_B:'', Value_c:'', Value_D:'', Value_e:''}) The…
0
votes
0 answers

object destructing from mongodb with undefined length

enter code hereI have a created a shoppingCart app and I have to send the order to the client in email using nodemailer I cannot destructure this object this object can have many similar values like this {"5e4083f2c3231acf1ded8b1f": {"item": { …
0
votes
2 answers

React Native APIs - why use destructuring with just one parameter?

I'm studying JavaScript, React Native, and React Navigation. I have just learned about destructuring. It's great, except I don't see the point when there is only one parameter. For example, why not just write function HomeScreen( navigation ) { /*…