空数组和排序按键 JavaScript

Empty Arrays and Sorting Pushed Keys JavaScript

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

然后我想根据两个键对企业索引进行排序 - "name"(按字母顺序)和 "upVotes"。

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

var service = {
  create: create,
  businesses: [],
  upVote: upVote,
  showAllBiz: showAllBiz,

};
function showAllBiz(){
  $http.get("/api/businesses")
  .then(function(res) {
    service.businesses = res.data;
  }, function(err) {
      $log.info(err);
  });
}
 function create(data) {
  return $http({
    method: 'POST',
    url:    '/api/businesses',
    data:   data,
    headers: {
      'Content-Type': 'application/json'
    }
  }).then(function(res) {
      service.businesses.push(res.data);
  });
}

我也试过在后端sort(),没有结果。这是它的样子:

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 的 thenBy.js,但有点力不从心。

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

假设我理解了这个问题,下面是一些虚构数据的要点。为了(希望)提高可读性,我让排序函数更加冗长。

请注意,在排序器方法中,我们首先比较 name,然后是 upVotes,然后只返回 0 以表示对象相等。由于我们先比较 name,然后比较 upVotes,这相当于按 name,然后 upVotes 排序。

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.