Hi Stack Overflow community!
I have the following test array:
let test = [{
"searchQuery": "test",
"resultsLoaded": -1,
"offset": -1,
"allResults": 10
}, {
"metaData": {
"type": "page-content",
"image": true,
"name": "lorem-ipsum",
"rank": 10,
"tags": ["website:lorem/ipsum"]
},
"componentData": {
"header": "Lorem Ipsum",
"path": "/content/asdf",
"breadcrumbDouble": {
"breadcrumbItem1": {
"href": "lorem ipsum",
"text": "lorem ipsum"
},
"breadcrumbItem2": {
"href": "/content/asdf",
"text": "Lorem Ipsum"
}
},
"content": {
"subheadline": "Lorem Ipsum",
"img": {
"alt": "",
"imgSrc": "/content/dam/production/images/asdf.jpeg.imgTransformer/listPageItem/desktop/1511962617778/asdf.jpg"
},
"tags": [{
"category": "lorem",
"value": "ipsum"
}]
}
}
}, {
"metaData": {
"type": "page-content",
"image": true,
"name": "lorem-ipsum",
"rank": 50,
"tags": ["website:lorem/ipsum"]
},
"componentData": {
"header": "Lorem Ipsum",
"path": "/content/asdf",
"breadcrumbDouble": {
"breadcrumbItem1": {
"href": "lorem ipsum",
"text": "lorem ipsum"
},
"breadcrumbItem2": {
"href": "/content/asdf",
"text": "Lorem Ipsum"
}
},
"content": {
"subheadline": "Lorem Ipsum",
"img": {
"alt": "",
"imgSrc": "/content/dam/production/images/asdf.jpeg.imgTransformer/listPageItem/desktop/1511962617778/asdf.jpg"
},
"tags": [{
"category": "lorem",
"value": "ipsum"
}]
}
}
}, {
"metaData": {
"type": "page-content",
"image": true,
"name": "lorem-ipsum",
"rank": 30,
"tags": ["website:lorem/ipsum"]
},
"componentData": {
"header": "Lorem Ipsum",
"path": "/content/asdf",
"breadcrumbDouble": {
"breadcrumbItem1": {
"href": "lorem ipsum",
"text": "lorem ipsum"
},
"breadcrumbItem2": {
"href": "/content/asdf",
"text": "Lorem Ipsum"
}
},
"content": {
"subheadline": "Lorem Ipsum",
"img": {
"alt": "",
"imgSrc": "/content/dam/production/images/asdf.jpeg.imgTransformer/listPageItem/desktop/1511962617778/asdf.jpg"
},
"tags": [{
"category": "lorem",
"value": "ipsum"
}]
}
}
}];
What I want to achive now is to sort the objects by metaData.rank from highest to lowest.
I wanted to use lodash's orderBy() like this:
let result = orderBy(test, function(e) {
let array = parseInt(e.metaData.rank);
return array;
}, ['desc']);
But I get the following error:
TypeError: Cannot read property 'rank' of undefined
console.log(e) in my orderBy function shows me this:
{
searchQuery: 'test',
resultsLoaded: -1,
offset: -1,
allResults: 10
}
Any quick suggestions to fix this?
I know that the first object is causing the problem because of the different structure. So I need to work around it!