Search code examples
javascriptarraysecmascript-6loopbackecmascript-2016

How do I access the nested properties of an array in Loopback?


here,
res = Array of results retrieved

res = [ Object, Object, Object... Object ]

Each Object looks like this:

Object{
    "userId": "ab1ce",
    "groupId": "a1de2"
}

Now, the results has an array of objects like this, how do i access the userId present in those Objects?


Solution

  • If you're using it in loopback you can use the inq property in the where clause so as to search something from inside the Array e.g:

    modelname.method({ where: { "property": { inq [ res.property  ] } } }, (err, res) => {
        if(err) { return console.error(err);
        } else {
            return true;
        }
    });
    

    The inq operator checks whether the value of the specified property matches any of the values provided in an array. The general syntax is:

    {where: { property: { inq: [val1, val2, ...]}}}
    

    Where:

    property is the name of a property (field) in the model being queried.

    val1, val2, and so on, are literal values in an array.

    Example of inq operator:

    Posts.find({where: {id: {inq: [123, 234]}}}, 
        function (err, p){... });
    

    Answer is Specific to Loopback.