最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - fat arrow with objects - Stack Overflow

programmeradmin1浏览0评论

Is there a way to use a fat arrow with an object?

The following code prints out the contents of the array "test" in the console.

//With array
let test = [1, 2, 3, 4];
test.forEach(number => console.log(number));

I'm looking for a way to have the same output but with "test" being an object, not an array (like below). is there a (relatively) simple way of doing this?

//With object
let test = {
  a: 1,
  b: 2,
  c: 3,
  d: 4
}
test.forEach(number => console.log(number));

Is there a way to use a fat arrow with an object?

The following code prints out the contents of the array "test" in the console.

//With array
let test = [1, 2, 3, 4];
test.forEach(number => console.log(number));

I'm looking for a way to have the same output but with "test" being an object, not an array (like below). is there a (relatively) simple way of doing this?

//With object
let test = {
  a: 1,
  b: 2,
  c: 3,
  d: 4
}
test.forEach(number => console.log(number));
Share Improve this question asked Jun 21, 2018 at 8:34 Simon AlexanderSimon Alexander 134 bronze badges 3
  • ES6 arrows are used to make arrow functions : developer.mozilla/en-US/docs/Web/JavaScript/Reference/… Therefore, you can use them as you would with a usual function – Seblor Commented Jun 21, 2018 at 8:37
  • 3 Possible duplicate of Iterate through object properties – Liam Commented Jun 21, 2018 at 8:38
  • 1 An arrow function is just a short hand, your issue here isn't the arrow function it's your loop. – Liam Commented Jun 21, 2018 at 8:38
Add a ment  | 

5 Answers 5

Reset to default 8

There is a couple of ways to do this:

Object.keys(test).forEach(key => console.log(test[key]));

Object.keys is the oldest method, available since ES5.

However, as you're using ES6 method you can probably use newer methods:

Object.keys(test) // ['a', 'b', 'c', 'd']
Object.values(test) // [1, 2, 3, 4]
Object.entries(test) // [['a', 1], ['b', 2], ['c', 3], ['d', 4]]

Array methods cannot be used on an object. Use Object.keys to get an array of object's keys, then use array method forEach

let test = {
  a: 1,
  b: 2,
  c: 3,
  d: 4
}
Object.keys(test).forEach(number => console.log(test[number]));

for array you can use array.forEach() and for object you need to use Object.values(object).forEach() for values and Object.keys(object).forEach() for object keys. :D

//With array
var test = [1, 2, 3, 4];
console.log("Array");
test.forEach(number => console.log(number));

console.log("Obejct");
//With object
var testObj = {
  a: 1,
  b: 2,
  c: 3,
  d: 4
}
Object.values(testObj).forEach(number => console.log(number));

Or you can use Object.values()

let test = {
  a: 1,
  b: 2,
  c: 3,
  d: 4
}
Object.values(test).forEach(number => console.log(number));

You have following options

1) Object.keys()

test = {
  a: 1,
  b: 2,
  c: 3,
  d: 4
}

Object.keys(test).forEach(key => console.log(test[key]))

Object.keys returns an array of keys of the object it was called on.

2) Object.values()

test = {
   a: 1,
   b: 2,
   c: 3,
   d: 4
}

Object.values(test).forEach(value=> console.log(value))

Object.values Returns an array whose elements are the enumerable property values found on the object.

发布评论

评论列表(0)

  1. 暂无评论