0

json Structure:

    {
    "id" : "1",
    "Data" : [
    {
            "name" : "abc",

        },
        {
            "name" : "option1",
            "position" : [
                {
                    "name" : "option1",
                    "status" : [
                        {
                            "code" : "0",
                            "value" : "OFF"
                        },
                        {
                            "code" : "1",
                            "value" : "ON"
                        }
                    ]
                }]
        }   ]   
}

Here,I want to get the data from above complex Json structure.How to do that, Have tried below code but giving error like; error: uncaughtException: Cannot read property 'status' of undefined

function myfunc(req,res){
var collectionname  = db.collection("col1");
collectionname.find({}).each(function(err, doc) {
   if(doc != null)
    {
        var fdata = [];
        for(var i =0;i<doc.Data.length;i++){
            fdata.push(doc.Data[i].position.status);
            }
        console.log("fdata............",fdata);
    }
});
}

Please help with the same.

5
  • 2
    Position is an array, so can't access it using position.status. You can use position[0].status if there is only one object in the position array Commented Mar 12, 2018 at 9:39
  • doc.Data[i].position is an array Commented Mar 12, 2018 at 9:39
  • updated my question. Commented Mar 12, 2018 at 10:12
  • @TB.M doc.Data[i].position[0].status But you need to think a bit about this. The fact that it's an array means that it can contain any number (including 0) of objects with or without a status property. Can you be sure there's always at least one object? If there are more than one, can you be sure the first one is always the one you want? Commented Mar 12, 2018 at 10:19
  • yeah,It will not always 0 element,I hav updated the array in question and below i got the answer also.Thanks for understanding. Commented Mar 12, 2018 at 10:24

3 Answers 3

1

You can use foreach for prevent length undefined.

function myfunc(req,res)
{
    let collectionname = db.collection("col1");
    collectionname.find({}).each(function(err, doc)
    {
        if(doc != null)
        {
            let fdata = [];

            for(let i in doc.Data)
            {
                for(let j in doc.Data[i].position)
                {
                    fdata.push(doc.Data[i].position[j].status);
                }
            }

            console.log("fdata............", fdata);
        }
    });
}

@MikaelLennholm have right, for(let i in doc.Data) works but not recommanded, be careful not to use it in prototyped or object-built arrays.


EDIT:

function myfunc(req,res)
{
    db.collection('col1').find({}).each(function(err, doc)
    {
        if(err)
        {
            console.log('[INFOS] Request fail, more details:\n', err);
        }
        else if(doc)
        {
            let fdata = [];

            if(doc.Data && doc.Data.length)
            {
                for(let i = doc.Data.length-1; i >= 0; i--)
                {
                    if(doc.Data[i].position && doc.Data[i].position.length)
                    {
                        for(let j = doc.Data[i].position.length-1; j >= 0; j--)
                        {
                            if(doc.Data[i].position[j].status)
                            {
                                fdata = fdata.concat(doc.Data[i].position[j].status);
                            }
                        }
                    }
                }
            }

            console.log("[INFOS] Datas:\n", fdata);
        }
    });
}
Sign up to request clarification or add additional context in comments.

12 Comments

And if we want to include name along with status .how we can do?
NEVER use in for arrays, it's going to cause weird bugs
Than,how we can archive the result?
I m using In but it is not iterating,,I want to iterate the array ??How we can do that ?
For add name, you can just edit fdata.push(doc.Data[i].position[j].status); by fdata.push(doc.Data[i].position[j]);
|
1

im a newbie at nodejs, hope this's correctly

//I assume this is a object or you can convert from string to object
var data = {
    "id": "1",
    "Data": [
        {
            "name": "option1",
            "position": [
                {
                    "name": "option1",
                    "status": [
                        {
                            "code": "0",
                            "value": "OFF"
                        },
                        {
                            "code": "1",
                            "value": "ON"
                        }
                    ]
                }]
        }]
}

var statusArr = data.Data[0].position[0].status;
console.log(...statusArr);

Result: { code: '0', value: 'OFF' } { code: '1', value: 'ON' }

1 Comment

but same way I am doing it is not happening..If u check in my code.
0

Parse through position array as well

function myfunc(req,res){
var collectionname  = db.collection("col1");
collectionname.find({}).each(function(err, doc) {
   if(doc != null)
    {
        var fdata = [];

        for(var i =0;i<doc.Data.length;i++){
        for(var j =0;j<doc.Data[i].position.length;j++){
            fdata.push(doc.Data[i].position[j].status);
            }
        }
        console.log("fdata............",fdata);
    }

}); }

2 Comments

error: uncaughtException: Cannot read property 'length' of undefined.
is position array always,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.