-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
  • 34,372
  • 9
  • 61
  • 83
Rahul Saini
  • 927
  • 1
  • 11
  • 28

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
  • 34,372
  • 9
  • 61
  • 83
0

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

Robert
  • 78
  • 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
  • 2,088
  • 2
  • 19
  • 24
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
  • 811
  • 1
  • 8
  • 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,800
  • 3
  • 30
  • 45
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
  • 4,206
  • 16
  • 45