空数组和排序推送键 JavaScript

Empty Arrays and Sorting Pushed Keys JavaScript

本文关键字:JavaScript 排序 数组      更新时间:2023-09-26

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

然后,我想根据两个键对企业索引进行排序 - "名称"(按字母顺序)和"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);
  });
}

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

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 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.