[英]Sort objects in array by key with lodash
嗨Stack Overflow社区!
我有以下测试数组:
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"
}]
}
}
}];
我现在想要的是按metaData.rank
从最高到最低排序对象。
我想像这样使用lodash的orderBy()
:
let result = orderBy(test, function(e) {
let array = parseInt(e.metaData.rank);
return array;
}, ['desc']);
但是我收到以下错误:
TypeError:无法读取未定义的属性“rank”
我的orderBy
函数中的console.log(e)
显示了这个:
{
searchQuery: 'test',
resultsLoaded: -1,
offset: -1,
allResults: 10
}
有任何快速建议来解决这个问题
我知道第一个对象是因为结构不同而导致问题。 所以我需要解决它!
数组中的第一个元素,
{
"searchQuery": "test",
"resultsLoaded": -1,
"offset": -1,
"allResults": 10
}
, 没有属性metaData
,并且由于还没有rank
通过对它进行排序。
您可能希望在排序之前将其从数组中删除,或者将其视为身份函数中的特殊情况,例如:
function(e) {
return !e.metaData
? -1
: parseInt(e.metaData.rank);
}
您可以将没有metaData
属性的项目移动到顶部,并按rank
对其余项目进行rank
。
var array = [{ 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" }] } } }]; array.sort(function (a, b) { return ('metaData' in a) - ('metaData' in b) || b.metaData.rank - a.metaData.rank; }); console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
数组test
的第一个对象导致了该问题。 它与其他结构的结构不同。 如果e
是第一个对象,那么e.metaData
是undefined
。
我建议你shift
是第一个对象, sort
数组,然后unshift
回数组排序:
let test = [...];
let first = test.shift(); // remove the first object from the array and store it in 'first'
test.sort((a, b) => b.metaData.rank - a.metaData.rank); // sort the array (you can use orderBy here if you want)
test.unshift(first); // unshift the first object (return it back to the first index of the array)
注意:我建议的解决方案假设只有一个不同的对象,并且该对象始终位于索引0
。
相反, e.metaData.rank
使用:
_.get(e, ['metaData', 'rank'], /*value if rank field is not exist*/ -1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.