5

Let's say I create object like this:

updates.push({
      id: this.ids[i],
      point: point,
      value: value
    });

Later on I want to use JSON.stringify on updates object, however I need only point and value like:

updates[{point: 1, value: 12}, {point: 2, value: 24}]

What's the best ES6 solution for that?

I looked at some examples with delete, but that's not what I actually need, as I do not want to delete ids.

9
  • So clone it and delete it or just copy what you need. Commented Dec 20, 2018 at 19:05
  • you simply clone the object ignoring certain fields before stringify Commented Dec 20, 2018 at 19:05
  • JSON.stringify(value[, replacer[, space]]), leverage replacer Commented Dec 20, 2018 at 19:06
  • 1
    Please clarify: Do you specifically just want point and value, always? Or do you want every property except ID? Commented Dec 20, 2018 at 19:13
  • Only point and value, always Commented Dec 20, 2018 at 19:14

3 Answers 3

7

Try the following :

JSON.stringify(updates.map(({point,value})=>({point,value})));

let updates = [{id : 1, point : 1, value: 2},{id : 1, point : 1, value: 2}];
console.log(JSON.stringify(updates.map(({point,value})=>({point,value}))));

Sign up to request clarification or add additional context in comments.

Comments

4

If updates is an array. Then you might want something like this:

const newArrayWithoutId = updates.map(({ point, value }) => {
  return {
    point,
    value,
  }
}

7 Comments

({id, ...rest}) => ({...rest}) would be cleaner still.
i think it depends on what you're trying to do.. but i agree. I would make the argument that my version is more readable and more specific. If the application changes and updates items starts to contains more properties. You would get all of them, when possibly the OP just wants the point and value data points only.
@tylerroper makes a good point in the other answer post though.
I'm not sure I agree about readability, but yes, the correct answer depends a bit more on the real-wold scenario. I make the same point in a comment on my own answer.
@TylerRoper: No worries. I'm keeping mine here after all the conversation related to it, but I upvoted both of the other current answers.
|
3

Just ({id, ...rest}) => ({...rest}) is too short for an answer, so how about this?

const withoutId = ({id, ...rest}) => ({...rest})

const vals = [
  {id: 'a', point: 1, value: 'foo'},
  {id: 'b', point: 2, value: 'bar'},
  {id: 'c', point: 3, value: 'baz', meaning: 42}
]

const reduced = vals.map(withoutId)

console.log(reduced)

2 Comments

OP's title is about "removing a property", so I think the real solution should provide "Everything except id". This is the only answer so far that has achieved that without hard-coding any property names. +1 from me - though this is all based on speculation unfortunately. OP will need to clarify.
Yes, although it's never clear with small examples whether the real-world case would involve removing a few or keeping a few values, or worst, somewhere in between.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.