javascript - How to iterate through array of objects in ES6 - Stack Overflow

Trying to iterate array of objects using es6 as it is very new for me here is my array of objects[j]0:

Trying to iterate array of objects using es6 as it is very new for me

here is my array of objects

[j]
0: j
$extCollectionIndex: 0
data: {ID: "b7f7ce8b-1455-41b3-ac26-b54916f6718f", userId: "444441", userName: "cjtest.1", email: "[email protected]",  …}

need to return or console username

I just tried(map and find )

let obj = records.map(obj => {return obj.data});

console.log(obj)//[object,object]

can any one help me on this

Trying to iterate array of objects using es6 as it is very new for me

here is my array of objects

[j]
0: j
$extCollectionIndex: 0
data: {ID: "b7f7ce8b-1455-41b3-ac26-b54916f6718f", userId: "444441", userName: "cjtest.1", email: "[email protected]",  …}

need to return or console username

I just tried(map and find )

let obj = records.map(obj => {return obj.data});

console.log(obj)//[object,object]

can any one help me on this

Share Improve this question edited Jan 4, 2019 at 14:02 Kon 4,0994 gold badges25 silver badges38 bronze badges asked Jan 4, 2019 at 14:00 R9102R9102 7271 gold badge11 silver badges35 bronze badges 6
  • can you post the whole array? – Harish Soni Commented Jan 4, 2019 at 14:02
  • ...obj.data.userName – gaetanoM Commented Jan 4, 2019 at 14:03
  • What's wrong with map ? – Weedoze Commented Jan 4, 2019 at 14:03
  • Your question is pretty vague, but I'd assume you need something like for (let object of records) { console.log(object.data.username); } – Bertijn Pauwels Commented Jan 4, 2019 at 14:03
  • 3 records.map(obj => obj.data.username); – Weedoze Commented Jan 4, 2019 at 14:04
 |  Show 1 more ment

3 Answers 3

Reset to default 3

Array.prototype.map will return a new array. If you return obj.data you will have an array of objects. You need to be more specific about the data you need.

let obj = records.map(obj => obj.data.userName );

Just use your map function over record.data.userName and not just record.data, you can then print it out using join. Or use a forEach loop with a console.log inside.

Working example :

function foo(){
  const records = [
      {
          "data": {
              "ID": "b7f7ce8b-1455-41b3-ac26-b54916f6718f",
              "userId": "444441",
              "userName": "cjtest.1",
              "email": "[email protected]"
          }
      },
      {
          "data": {
              "ID": "b7f7ce8b-1455-41b3-ac26-b54916f6718f",
              "userId": "444441",
              "userName": "srtkjrthrt",
              "email": "[email protected]"
          }
      },
      {
          "data": {
              "ID": "b7f7ce8b-1455-41b3-ac26-b54916f6718f",
              "userId": "444441",
              "userName": "srthstrj",
              "email": "[email protected]"
          }
      },
      {
          "data": {
              "ID": "b7f7ce8b-1455-41b3-ac26-b54916f6718f",
              "userId": "444441",
              "userName": "cjghj1",
              "email": "[email protected]"
          }
      }
  ]
  const userList = records.map(record => record.data.userName)
  console.log(userList.join(', '))
}

foo()

Here is the output

let obj = records.map(obj => {return obj.data.username});

console.log(obj)//cjtest.1

Thanks you @Weedoze @gaetanoM

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744845173a4596790.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信