我的Node服务器中有一个包含数千个对象的对象,每个对象都是一个人,并且他有一个总分数。 它看起来像这样:
var mainObject = { //it's unsorted, but no need to sort it anyway
john: {totalScore: 5, info: "No"},
james: {totalScore: 3, info: "No"},
lee: {totalScore: 55, info: "No"}
}
现在,我想在mainObject中保留1000个对象,但问题是我需要在发送到客户端之前使用delete mainObject [name]擦除最低分数(除了delete之外的其他解决方案是可接受的)。
mainObject中每个对象的索引不应该相互切换(有一个原因),只需要得到最低分数,列表应该作为对象发送到客户端并且确切地说在1000长度。
答案 0 :(得分:1)
您正在尝试将基于数组的概念(如长度和排序)应用于Object。这根本不会。
要实现您的目标,您必须将对象转换为数组。你可以这样做的一种方式是:
var mainObject = {
john: {totalScore: 5, info: "No"},
james: {totalScore: 3, info: "No"},
lee: {totalScore: 55, info: "No"},
};
var mainArray = Object.keys(mainObject).map(function (name) {
return Object.assign({}, mainObject[name], {name});
});
console.log(mainArray);
然后,您可以按分数对数组进行排序,并仅返回N个最高条目:
var mainArray = [
{
"totalScore": 5,
"info": "No",
"name": "john"
},
{
"totalScore": 3,
"info": "No",
"name": "james"
},
{
"totalScore": 55,
"info": "No",
"name": "lee"
}
]
mainArray.sort(function (a, b) {
return b.totalScore - a.totalScore;
});
console.log(mainArray.slice(0, 1000));
如果由于某种原因你的客户绝对需要来取回一个Object而不是一个对象数组,你总是可以转换回来:
var mainArray = [
{
"totalScore": 5,
"info": "No",
"name": "john"
},
{
"totalScore": 3,
"info": "No",
"name": "james"
},
{
"totalScore": 55,
"info": "No",
"name": "lee"
}
];
var mainObject = mainArray.reduce(function (prev, curr) {
var name = curr.name;
delete curr.name;
prev[name] = curr;
return prev;
}, {});
console.log(mainObject);
但是在这一点上,你已经做了相当荒谬的工作来实现你的目标。它暗示的足够的工作实际上是an XY Problem,你应该重新考虑导致你到这一步的决定。
答案 1 :(得分:0)
使用lodash
var desiredAmountOfObjects = 2
var mainObject = { //it's unsorted, but no need to sort it anyway
john: {totalScore: 5, info: "No"},
james: {totalScore: 3, info: "No"},
lee: {totalScore: 55, info: "No"}
}
var mainArray = _.map(mainObject, (value, key) => Object.assign(value, { key }))
mainArray.sort(function (a, b) {
return b.totalScore - a.totalScore;
});
var choppingBlock = mainArray.slice(desiredAmountOfObjects).map(chop => chop.key)
console.log(choppingBlock);
var desiredObject = _.omitBy(mainObject, (value, key) => choppingBlock.indexOf(key) > -1)
console.log(desiredObject)
http://jsbin.com/gijuduzole/1/edit?js,console
将desiredAmountOfObject设置为1000或其他。
答案 2 :(得分:0)
您可以创建一个删除分数最低的新对象 Object.keys和Array迭代方法:
const mainObject = {
john: {totalScore: 5, info: "No"},
james: {totalScore: 3, info: "No"},
lee: {totalScore: 55, info: "No"}
};
const keys = Object.keys(mainObject);
const lowest = keys.map(key => mainObject[key].totalScore).sort()[0];
const removed = keys.reduce((result, key) => {
if(mainObject[key].totalScore > lowest){
result[key] = mainObject[key];
}
return result;
}, Object.create(null));
console.log('without objects with lowest score');
console.log(removed);
// EDIT -- Or, to retain keys:
const removed2 = keys.reduce((result, key) => {
result[key] = mainObject[key].totalScore > lowest
? mainObject[key]
: undefined;
return result;
}, Object.create(null));
console.log('retaining all keys');
console.log(removed2);
答案 3 :(得分:0)
根据评论 - 从初始对象创建一个可以排序的数组,然后获取除x结果之外的所有结果,并从初始对象中删除这些结果。
// object
var mainObject = { //it's unsorted, but no need to sort it anyway
john: {totalScore: 5, info: "No"},
james: {totalScore: 3, info: "No"},
lee: {totalScore: 55, info: "No"}
}
// array
var array = [];
for (var i in mainObject)
array.push({ key: i, score: mainObject[i].totalScore });
// sort
array.sort(function(a,b){ return a.score < b.score });
// keep top x
var remove = array.slice(2);
// delete all the others
for (var i=0; i<remove.length; i++)
delete mainObject[remove[i].key];
console.log(mainObject);
答案 4 :(得分:-2)
有很多图书馆可以帮助你解决这类问题(下划线,lodash,ramda)。
使用ramda的一个例子:
const mainObject = { //it's unsorted, but no need to sort it anyway
john: {totalScore: 5, info: "No"},
james: {totalScore: 3, info: "No"},
lee: {totalScore: 55, info: "No"}
}
const obj = R.filter((person) => person.totalScore > 50, mainObject);
这样您就不必担心转换为数组,只需应用过滤器即可获得对象。