-2

This is the structure which is saved in the database. I want to fetch only "entityInfo" directly without using any loop.

let x = {
    "12": [{
            "entity": {
                "id": "40ea18e6-f898-414a-96fd-b3ef5a0eb7cd"
            },
            "startTime": "2018-12-19T06:29:59.999Z"
        },
        {
            "entity": {
                "id": "40ea18e6-f898-414a-96fd-b3ef5a0eb7cd"
            },
            "startTime": "2018-12-19T06:49:25.000Z",

        },
        {
            "entityInfo": [{
                "entityName": "acd",
                "timeSpent": 0.028055555555555556
            }]
        }
    ]
}
adiga
  • 31,610
  • 8
  • 53
  • 74
Rahul Saini
  • 895
  • 1
  • 10
  • 23

6 Answers6

3

If you want the first item in the 12 array which has an entityInfo value, then you can use find

let x = {
    "12": [{
            "entity": {
                "id": "40ea18e6-f898-414a-96fd-b3ef5a0eb7cd"
            },
            "startTime": "2018-12-19T06:29:59.999Z"
        },
        {
            "entity": {
                "id": "40ea18e6-f898-414a-96fd-b3ef5a0eb7cd"
            },
            "startTime": "2018-12-19T06:49:25.000Z",

        },
        {
            "entityInfo": [{
                "entityName": "acd",
                "timeSpent": 0.028055555555555556
            }]
        }
    ]
}

console.log(x["12"].find(a => a.entityInfo))
adiga
  • 31,610
  • 8
  • 53
  • 74
0

You can use the map() function encapsulated. Click here. But you have to question yourself if an array makes sense here.

Robert
  • 80
  • 1
  • 8
0

let x = {
    "12": [{
            "entity": {
                "id": "40ea18e6-f898-414a-96fd-b3ef5a0eb7cd"
            },
            "startTime": "2018-12-19T06:29:59.999Z"
        },
        {
            "entity": {
                "id": "40ea18e6-f898-414a-96fd-b3ef5a0eb7cd"
            },
            "startTime": "2018-12-19T06:49:25.000Z",

        },
        {
            "entityInfo": [{
                "entityName": "acd",
                "timeSpent": 0.028055555555555556
            }]
        }
    ]
}
// will return a value if the entityInfo object exists
var ans = x["12"].filter((val)=>{return val.entityInfo})[0]
console.log(ans)
Tiisetso Tjabane
  • 1,920
  • 2
  • 18
  • 22
0

If there only is one entityInfo you can use the following to get the first element that has a "entityInfo" property.

x["12"].find(i => i.entityInfo)
Kali
  • 671
  • 1
  • 6
  • 17
0

Use Object.values for getting the values and then find for finding all the entity info objects

var x = {
    "12": [{
            "entity": {
                "id": "40ea18e6-f898-414a-96fd-b3ef5a0eb7cd"
            },
            "startTime": "2018-12-19T06:29:59.999Z"
        },
        {
            "entity": {
                "id": "40ea18e6-f898-414a-96fd-b3ef5a0eb7cd"
            },
            "startTime": "2018-12-19T06:49:25.000Z",

        },
        {
            "entityInfo": [{
                "entityName": "acd",
                "timeSpent": 0.028055555555555556
            }]
        }
    ]
};

console.log(Object.values(x).flat().find(el => el.entityInfo));
quirimmo
  • 9,530
  • 2
  • 27
  • 43
0

There are multiple approaches and possible solutions to this question. You can use map, filter, reduce, find , forEach. But each of the loop over elements internally, the other approach is if you know the position of entityInfo in x["12"] and you want to safe read it. Then you can use utilities like these.

Ashish
  • 3,828
  • 15
  • 40