4

Im writing some code atm with ES6 and Babel (es2015 preset) and I cant spread one Object as I am used to. Normally I would take an Object, use a spread and map the inner content like [...someObject].map(dosomestuff). But my one object does not behave as expected and the only difference I found so far are the keys:

let myObject = {
  'key': content,
  'key2': content,
  'key3': content
};

let array = [...myObject];

Since the object gets generated form a file structure, the keys are formed by variables and can include special chars, so i need to set them like object[key] = value. Why cant I use the spread operator on that object (the array is always empty)? And is there a workaround thats as comfortable as the spread-operator (I dont mean making a new Array with Object.keys and use that)

4
  • 1
    What is the desired result here? Commented May 18, 2017 at 20:50
  • I want get an array that i can map like [content, content, content].map(doSomething) Commented May 18, 2017 at 20:53
  • You can map() keys to values or use Object.values(myObject) Commented May 18, 2017 at 20:54
  • 1
    ... is not an operator! "Why cant I use the spread operator on that object" You can only use a spread element when that element is iterable. Objects are not iterable. Commented May 18, 2017 at 21:01

3 Answers 3

6

The object spread works on bojects, you can't spread an object into an array.

You should spread it to another object:

let myObject = {
  'key': "content1",
  'key2': "content2",
  'key3': "content3"
};

let myObject2 = {
  'key4': 'content4'
}

let newObj= {...myObject, ...myObject2};
console.log(newObj);

This is not part of ES6, but it is on stage 3

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

3 Comments

This is invalid ES6.
Sure, but how should somebody deduce that from your answer?
2

Why cant I use the spread operator on that object?

First of all, ... is not an operator!.

You can only use a spread element when that element is iterable. Objects are not iterable.

And is there a workaround thats as comfortable as the spread-operator (I dont mean making a new Array with Object.keys and use that)

Not in ES6.

I am assuming that you want to get the property values from the object.

If you don't wan to write a helper function and don't necessarily want to use a spread element, you can actually make use of Array.from's ability to take a mapping function as second argument:

Array.from(Array.keys(myObject), x => myObject[x]);

If you really don't want to use Object.keys (Object.values doesn't exist in ES6), then you can write yourself your own helper function as a generator:

function* values(obj) {
  for (var x in obj) {
    // You can use `obj.hasOwnProperty(x)` to guard against inherited properties
    // to achieve the same effect as `Object.keys`
    yield obj[x];
  }
}

let myObject = {
  'key': 'content',
  'key2': 'content1',
  'key3': 'content2'
};

console.log([...values(myObject)]);
// or
console.log(Array.from(values(myObject)));

Comments

0

With Chrome I see that using spread on an object results in an exception. What does work is using Map

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.