3

I have in the state most of the item I wanted to push it in an array

and I want to push all state to except some item

so here's my state

state = {
    username: '..',
    date: '..',
    time: '..',
    description: '..',
    images: '..',
    buildingNumber: '..',
    status: '..',
    serviceDB: '..',
    snapshotKey: '..', // i don't need this when pushed
    count: -1, // i don't need this when pushed
  };

here's my code

 let order = [];
    order.push(this.state);
    console.log(order); it's log all state 

// I want to push it to DB
    database()
      .ref(`Providers/ProvidersOrders/${uid}`)
      .push(...order);
  };
Oliver D
  • 2,579
  • 6
  • 37
  • 80
  • `const { snapshotKey, count, ...filteredData } = this.state;` – Emile Bergeron Dec 17 '19 at 19:10
  • Does this answer your question? [Is there a way to destructure an object into a new object in es6?](https://stackoverflow.com/questions/50381557/is-there-a-way-to-destructure-an-object-into-a-new-object-in-es6) – Emile Bergeron Dec 17 '19 at 19:11
  • [Destructuring object and ignore one of the results](https://stackoverflow.com/q/37838778/1218980) – Emile Bergeron Dec 17 '19 at 19:16
  • Thank u @EmileBergeron so now `filteredData` should be an array of items needs right? – Oliver D Dec 17 '19 at 19:18
  • Nope, `filteredData` is a new object that only has the properties that were not declared explicitly when destructuring. – Emile Bergeron Dec 17 '19 at 19:20
  • so when I declare a `filteredData` it should be destructuring his properties from state like `const filterD = ({username,....}=state)` cuz when i do it i got a syntax error – Oliver D Dec 17 '19 at 19:36
  • Do _exactly_ like in [my first comment](https://stackoverflow.com/questions/59380600/how-can-i-push-the-state-to-an-array-with-specific-items?noredirect=1#comment104953223_59380600). – Emile Bergeron Dec 17 '19 at 19:37
  • OUCH,I think it should be declared before use it – Oliver D Dec 17 '19 at 19:39
  • Yes, [it's a statement, not an expression](https://stackoverflow.com/q/12703214/1218980), so it needs to be on it's own line. – Emile Bergeron Dec 17 '19 at 19:42
  • 3
    @EmileBergeron Really thank u very much I learn new things today :) – Oliver D Dec 17 '19 at 19:44

3 Answers3

1

You can use a destructuring assignment and the rest parameters syntax if you want to do it without using any library:

const { snapshotKey, count, ...rest } = this.state;

...

order.push(rest);

Otherwise, you can also use Lodash's _.omit function:

order.push(_.omit(this.state, ['snapshotKey', 'count']));

Alternatively, if you want to select which properties to use, you can use destructuring again and shorthand property names to create the object:

const {
    username,
    date,
    time,
    description,
    images,
    buildingNumber,
    status,
    serviceDB,
} = this.state;

...

order.push({
    username,
    date,
    time,
    description,
    images,
    buildingNumber,
    status,
    serviceDB,
});

Or, with Lodash, use _.pick, which is the opposite of _.omit:

order.push(_.pick(this.state, [
    'username',
    'date',
    'time',
    'description',
    'buildingNumber',
    'status',
    'serviceDB',
]));
Danziger
  • 19,628
  • 4
  • 53
  • 83
  • I'm not the one who downvoted, though your answer is exactly the same 3 ways described in the duplicate candidate. We shouldn't answer again if there is already good questions and answers on SO. – Emile Bergeron Dec 17 '19 at 19:40
  • @EmileBergeron I think downvotes should not be allowed without feedback, thus I will re-create answers anytime I find these situations where all posts get a no-comment downvote systematically. – Danziger Dec 17 '19 at 19:44
  • This will only get your account banned and annoy admins, I don't think it's a good idea in the long run. And forcing a comment when downvoting has already been [discussed](https://meta.stackoverflow.com/q/357436/1218980) [a](https://meta.stackoverflow.com/q/250177/1218980) [lot](https://meta.stackexchange.com/q/135/254800). – Emile Bergeron Dec 17 '19 at 19:47
  • 1
    (I finally chose to upvote anyway since this is the most complete answer here right now) – Emile Bergeron Dec 17 '19 at 20:07
0

If you want to select some properties then just declare properties that you want:

let {snapshotKey, count, ...y} = state;
arr.push(y);    

An example:

state = {
    username: '1',
    date: '2',
    time: '3',
    description: '4',
    images: '5',
    buildingNumber: '6',
    status: '7',
    serviceDB: '8',
    snapshotKey: '9', // i don't need this when pushed
    count: -10, // i don't need this when pushed
  };
let arr = [];
let {snapshotKey, count, ...y} = state;
arr.push(y);
console.log(arr);
StepUp
  • 36,391
  • 15
  • 88
  • 148
  • 2
    Your initial answer was invalid syntax. I removed my downvote, though the question should be closed as a dupe, there are already a lot of similar answers on SO. – Emile Bergeron Dec 17 '19 at 19:35
0

You can use Destructuring assignment As shown in the documentation

const {  count,snapshotKey, ...newData } = this.state;

Now, newData contains:

 newData={
    username: '..',
    date: '..',
    time: '..',
    description: '..',
    images: '..',
    buildingNumber: '..',
    status: '..',
    serviceDB: '..',
  };

now use in newData

 database()
      .ref(`Providers/ProvidersOrders/${uid}`)
      .push(...newData);

You can read more about it here and here also here plus great examples

Yoel
  • 7,555
  • 6
  • 27
  • 59