Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
空数组和按键排序JavaScript_Javascript_Arrays_Node.js_Sorting_Mean Stack - Fatal编程技术网

空数组和按键排序JavaScript

空数组和按键排序JavaScript,javascript,arrays,node.js,sorting,mean-stack,Javascript,Arrays,Node.js,Sorting,Mean Stack,我正在制作一个meanstack应用程序,并试图创建一个业务数据库——这需要一个空数组来将业务模型的新实例推送到其中 然后,我想根据两个键——“名称”(按字母顺序)和“向上投票”对企业索引进行排序 以下是我的business.service文件(客户端)中的内容: 我还尝试在后端对()进行排序,但没有结果。下面是它的样子: var Business = require("../models/business"); var nodemailer = require("nodemailer"

我正在制作一个meanstack应用程序,并试图创建一个业务数据库——这需要一个空数组来将业务模型的新实例推送到其中

然后,我想根据两个键——“名称”(按字母顺序)和“向上投票”对企业索引进行排序

以下是我的business.service文件(客户端)中的内容:

我还尝试在后端对()进行排序,但没有结果。下面是它的样子:

var Business    = require("../models/business");
var nodemailer  = require("nodemailer");
var transporter = nodemailer.createTransport();
var firstBy     = require("thenby");

function index(req, res) {
 if(req.query.search){
  Business.find({}).then(function(data) {
  var reg = new RegExp(req.query.search, "i");
  data = data.filter(function(biz) {
    if(reg.test(biz.name)) return biz
  })

  res.json(data);
  }, function(err) {
  res.json(err);
 });
 } else{
  Business.find({}).then(function(data) {
   res.json(data);
   }, function(err) {
   res.json(err);
  });
 }
}


function create(req, res) {
var business       = new Business();  

console.log(req.body);
business.name              = req.body.name;
business.address1          = req.body.address1;
business.address2          = req.body.address2;
business.email             = req.body.email;
business.twitterHandle     = req.body.twitterHandle;
business.upVote            = req.body.upVote;

business.save(function(err, savedBusiness) {
 if (err) {
  res.send(err)
 }

 res.json(savedBusiness);
});
我陷入了这样一个事实:我需要为新实例(在我的服务中)使用空数组,但我还需要在.sort()方法中使用数组中的对象来访问键(我想要排序)

我和Teun's打过球,但有点力不从心


我在谷歌上搜索了排序数组和对象数组,但这些都是排序现有信息的示例,而不是排序尚不存在的信息,因此需要使用空数组

假设我理解这个问题,下面是一些虚构数据的要点。为了(希望)增加可读性,我让sort函数更详细一些

请注意,在分类器方法中,我们首先比较
name
,然后比较
upvots
,然后只返回
0
,以表示对象相等。由于我们先比较
名称
,然后比较
向上投票
,这相当于按
名称
排序,然后再比较
向上投票

let arr = [
    { id: 1, name: 'Dominos', upVotes: 21 },
    { id: 2, name: 'Pizza Hut', upVotes: 31 },
    { id: 3, name: 'Pizza Hut', upVotes: 35 },
    { id: 4, name: 'Dominos', upVotes: 61 },
    { id: 5, name: 'Dominos', upVotes: 2 },
    { id: 6, name: 'Pizza Hut', upVotes: 25 },
    { id: 7, name: 'Dominos', upVotes: 10 },
    { id: 8, name: 'Pizza Hut', upVotes: 3 }
];

function sorter(a, b) {
    if (a.name > b.name)
        return 1;
    if (a.name < b.name)
        return -1;
    if (a.upVotes > b.upVotes)
        return 1;
    if (a.upVotes < b.upVotes)
        return -1;
    return 0;
}

arr.sort(sorter);

console.log(arr);
/* [ { id: 7, name: 'Dominos', upVotes: 2 },
     { id: 8, name: 'Dominos', upVotes: 10 },
     { id: 5, name: 'Dominos', upVotes: 21 },
     { id: 6, name: 'Dominos', upVotes: 61 },
     { id: 4, name: 'Pizza Hut', upVotes: 3 },
     { id: 3, name: 'Pizza Hut', upVotes: 25 },
     { id: 1, name: 'Pizza Hut', upVotes: 31 },
     { id: 2, name: 'Pizza Hut', upVotes: 35 } ] */

[].sort(sorter); // no errors. if the array is empty, the callback will never be run.
让arr=[
{id:1,名字:'多米诺骨牌',投票数:21},
{id:2,名字:'必胜客',投票:31},
{id:3,名字:'必胜客',投票:35},
{id:4,名字:'多米诺骨牌',投票:61},
{id:5,名字:'多米诺骨牌',投票数:2},
{id:6,名字:'必胜客',投票数:25},
{id:7,名字:'多米诺骨牌',投票数:10},
{id:8,名字:'必胜客',投票数:3}
];
功能分拣机(a、b){
如果(a.name>b.name)
返回1;
如果(a.nameb.UpVoces)
返回1;
如果(a.upvoces
:-)
。如果你不知道,编辑有时会阅读一篇文章,删减其中不重要的部分,和/或使其更具可读性。这里没什么可修的!好极了!谢谢你的信息:-)太棒了@dvlsg这适用于具有预制数据的阵列。它是以适当的顺序记录的。现在我想为service.business(创建的业务被推入的空数组)实现这一点,但最终得到一个空数组。这是我不清楚的部分-如何对尚未为其创建数据的数组进行排序?我可能需要一些额外的说明-您是否担心在尝试对空数组进行排序时抛出错误,或者您是否担心特定键(
name
向上投票
)的情况还没有定义吗?可能有助于查看需要排序时在
service.businesses
中的确切示例。业务的模型是
var businessSchema=new mongoose.Schema({name:{type:String},address1:{type:String},address2:{type:String},email:{type:String,required:false}),twitterHandle:{type:String,required:false},upVote:{type:Number,默认值:0},upvotters:[{type:String}]})
因此service.business将包含使用这些密钥添加到数据库中的新业务。我仍然有点迷茫。如果您想在内存中进行排序,无论它是否为空,无论是在服务器上还是在客户机上,我提供的排序器都将按原样工作。如果您想在数据库中进行排序(如果可能的话,我建议使用javascript排序),那么应该考虑使用服务器风格的
sort
。这是$sort in的文档,这是$sort in的文档。您可以使用
Business.find({}).sort({/*…*/})
fantactive来附加它。谢谢@dvlsg。我在内存端使用它,并得到一些结果!
let arr = [
    { id: 1, name: 'Dominos', upVotes: 21 },
    { id: 2, name: 'Pizza Hut', upVotes: 31 },
    { id: 3, name: 'Pizza Hut', upVotes: 35 },
    { id: 4, name: 'Dominos', upVotes: 61 },
    { id: 5, name: 'Dominos', upVotes: 2 },
    { id: 6, name: 'Pizza Hut', upVotes: 25 },
    { id: 7, name: 'Dominos', upVotes: 10 },
    { id: 8, name: 'Pizza Hut', upVotes: 3 }
];

function sorter(a, b) {
    if (a.name > b.name)
        return 1;
    if (a.name < b.name)
        return -1;
    if (a.upVotes > b.upVotes)
        return 1;
    if (a.upVotes < b.upVotes)
        return -1;
    return 0;
}

arr.sort(sorter);

console.log(arr);
/* [ { id: 7, name: 'Dominos', upVotes: 2 },
     { id: 8, name: 'Dominos', upVotes: 10 },
     { id: 5, name: 'Dominos', upVotes: 21 },
     { id: 6, name: 'Dominos', upVotes: 61 },
     { id: 4, name: 'Pizza Hut', upVotes: 3 },
     { id: 3, name: 'Pizza Hut', upVotes: 25 },
     { id: 1, name: 'Pizza Hut', upVotes: 31 },
     { id: 2, name: 'Pizza Hut', upVotes: 35 } ] */

[].sort(sorter); // no errors. if the array is empty, the callback will never be run.