我在尝试对复杂对象进行排序时遇到麻烦。这是对象结构:
[{
"searchResultProperties": [{
"key": "message_time",
"value": 1542088800000
}, {
"key": "size_byte AVG",
"value": 480
}, {
"key": "source_file",
"value": "log"
}, {
"key": "source_host",
"value": "lab8.domain.com"
}],
"show": false,
"key": null,
"type": null
}, {
"searchResultProperties": [{
"key": "message_time",
"value": 1542096000000
}, {
"key": "size_byte AVG",
"value": 373
}, {
"key": "source_file",
"value": "log"
}, {
"key": "source_host",
"value": "lab8.domain.com"
}],
"show": false,
"key": null,
"type": null
}, {
"searchResultProperties": [{
"key": "message_time",
"value": 1542103200000
}, {
"key": "size_byte AVG",
"value": 683
}, {
"key": "source_file",
"value": "log"
}, {
"key": "source_host",
"value": "lab8.domain.com"
}],
"show": false,
"key": null,
"type": null
}]
基本上,它是具有称为“ searchResultProperties”的属性的对象的数组,该属性是具有“键”和“值”属性的对象的数组。
我正在尝试对“键”属性进行排序,即:“ size_byte AVG”。
答案 0 :(得分:2)
您可以使用Array.prototype.sort()
函数,该函数为您提供两个对象,并且可以根据排序逻辑返回+ ve或-ve值。请参阅[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort]
array = [{
"searchResultProperties": [{
"key": "message_time",
"value": 1542088800000
}, {
"key": "size_byte AVG",
"value": 480
}, {
"key": "source_file",
"value": "log"
}, {
"key": "source_host",
"value": "lab8.domain.com"
}],
"show": false,
"key": null,
"type": null
}, {
"searchResultProperties": [{
"key": "message_time",
"value": 1542096000000
}, {
"key": "size_byte AVG",
"value": 373
}, {
"key": "source_file",
"value": "log"
}, {
"key": "source_host",
"value": "lab8.domain.com"
}],
"show": false,
"key": null,
"type": null
}, {
"searchResultProperties": [{
"key": "message_time",
"value": 1542103200000
}, {
"key": "size_byte AVG",
"value": 683
}, {
"key": "source_file",
"value": "log"
}, {
"key": "source_host",
"value": "lab8.domain.com"
}],
"show": false,
"key": null,
"type": null
}]
array = array.sort((a,b) => a.searchResultProperties.find(obj => obj.key === "size_byte AVG").value - b.searchResultProperties.find(obj => obj.key === "size_byte AVG").value )
console.log(array);
答案 1 :(得分:2)
您可以使用函数find
来查找键为size_byte AVG
的对象,并使用函数sort
来对数组进行排序。
此方法按升序排序
let arr = [{ "searchResultProperties": [{ "key": "message_time", "value": 1542088800000 }, { "key": "size_byte AVG", "value": 480 }, { "key": "source_file", "value": "log" }, { "key": "source_host", "value": "lab8.domain.com" }], "show": false, "key": null, "type": null}, { "searchResultProperties": [{ "key": "message_time", "value": 1542096000000 }, { "key": "size_byte AVG", "value": 373 }, { "key": "source_file", "value": "log" }, { "key": "source_host", "value": "lab8.domain.com" }], "show": false, "key": null, "type": null}, { "searchResultProperties": [{ "key": "message_time", "value": 1542103200000 }, { "key": "size_byte AVG", "value": 683 }, { "key": "source_file", "value": "log" }, { "key": "source_host", "value": "lab8.domain.com" }], "show": false, "key": null, "type": null}],
target = "size_byte AVG",
compare = (a, b) => a.find(({key}) => key === target).value - b.find(({key}) => key === target).value,
sorted = arr.sort(({searchResultProperties: a}, {searchResultProperties: b}) => compare(a, b));
console.log(sorted);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 2 :(得分:1)
sort接受两个值作为参数。通过find我们找到具有键'size_byte AVG'
的数组元素,并从中取出值并比较值
let arr = [{"searchResultProperties": [{"key": "message_time","value": 1542088800000}, {"key": "size_byte AVG","value": 480}, {"key": "source_file", "value": "log"}, {"key": "source_host","value": "lab8.domain.com"}],"show": false,"key": null, "type": null}, { "searchResultProperties": [{"key": "message_time","value": 1542096000000}, {"key": "size_byte AVG","value": 373}, {"key": "source_file","value": "log"}, {"key": "source_host","value": "lab8.domain.com"}],"show": false,"key": null, "type": null}, {"searchResultProperties": [{"key": "message_time","value": 1542103200000}, {"key": "size_byte AVG","value": 683 }, { "key": "source_file","value": "log"}, {"key": "source_host","value": "lab8.domain.com"}],"show": false,"key": null,"type": null}]
let op = arr.sort((a,b)=>
a.searchResultProperties.find(e=>e.key==='size_byte AVG').value -
b.searchResultProperties.find(e=>e.key==='size_byte AVG').value)
console.log(op)