Push element value from array of objects to a variable

前端 未结 4 1679
夕颜
夕颜 2021-01-25 10:33

I have some data from a collection named \"service\" which has data like this:

let service_data = [
  {
    \"name\":\"Service 1\",
    \"price\":60,
    \"resou         


        
4条回答
  •  北荒
    北荒 (楼主)
    2021-01-25 11:13

    Using .reduce:

    let service_data = [{
        "name": "Service 1",
        "price": 60,
        "resource_group_ids": ["5d5e5dea99d9b75ff2f78dcd", "5d5e85d329782914332368c8"]
      },
      {
        "name": "Service 2",
        "price": 60,
        "resource_group_ids": ["5d5e5dea99d9b75ff2f7cfe"]
      }
    ]
    
    const resource_groups = service_data.reduce(function(result, item) {
      result.concat(item.resource_group_ids)
    }, [])
    
    console.log(resource_groups)

提交回复
热议问题