3

I wan't remove one key. Look this

console.log(state);

i getting {1: {here is next object}}, next

const { 1: deletedValue, ...newState } = state;
console.log(newState);
console.log(state);

i getting

{1: {here is next object}}
{1: {here is next object}}

Removal does not work. I do not understand why

In the comment you invited to describe how the data looked more accurate:

state: {1: {id: 1, content: {name: "xyz", surname: "dsd"}},
2: {id: 2, content: {name: "abc", surname: "dsq"}}
}
Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
  • This isn't valid javascript: `{1: {here is next object}}`. Maybe you could edit the question to show what your data really looks like. – Mark Sep 04 '18 at 22:06
  • Please provide a runnable [mcve] that reproduces problem – charlietfl Sep 04 '18 at 22:07
  • Possible duplicate of [How do I remove a property from a JavaScript object?](https://stackoverflow.com/questions/208105/how-do-i-remove-a-property-from-a-javascript-object) – mpm Sep 04 '18 at 22:09
  • Problem solved by: Nina Scholz. I have edited to show exactly what the data looked like. – konradolejnik Sep 04 '18 at 22:22

1 Answers1

4

It looks like a babeljs problem.

The problem with a number as property for a destructuring assignment.

var object = { 1: 40, foo: 41, bar: 42, baz: 43 },
    { 1: y, foo: z, ...x } = object;
    //^
    
console.log(x);
console.log(y);
console.log(z);

Take a stringed number as target property instead of just the number.

var object = { 1: 40, foo: 41, bar: 42, baz: 43 },
    { '1': y, foo: z, ...x } = object;
    //^^^
    
console.log(x);
console.log(y);
console.log(z);
Community
  • 1
  • 1
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • i don't know. maybe a bug or it needs a string as key. – Nina Scholz Sep 04 '18 at 22:24
  • i have a look tomorrow. – Nina Scholz Sep 04 '18 at 22:25
  • Maybe babel error? It's true, I use: https://babeljs.io/docs/en/babel-plugin-transform-object-rest-spread. So i don't know. :/ – konradolejnik Sep 04 '18 at 22:32
  • 1
    @konradolejnik, with chrome it works as expected. i assume, it's a bug in the recognizing part of objects, where the key is stored as number, where it should be stored as string. – Nina Scholz Sep 05 '18 at 08:11
  • @konradolejnik, Nina: If that's the case, then the question and answer should be updated to explicitly call out the babel transform. E.g. I didn't know that you (Nina) actually enabled Babel for the code snippets. It works as expected in Firefox as well. – Felix Kling Sep 05 '18 at 21:13