Tag: 数组

错误的基于名称属性的对象数组

我有一个包含状态和名称属性的对象数组。 我想要使​​用他们的name属性使用下面的代码: array = array.sort(function(a,b){ return a.name – b.name }); 但是它不会返回正确的sorting数组。 这里有一个片段,你可以看到它的行动: var array = [{"name":"whoopsie","status":"active"},{"name":"acpid","status":"active"},{"name":"apache2","status":"active"},{"name":"apparmor","status":"active"},{"name":"apport","status":"active"},{"name":"avahi-daemon","status":"active"},{"name":"bluetooth","status":"active"},{"name":"cron","status":"active"},{"name":"cups","status":"active"},{"name":"cups-browsed","status":"active"},{"name":"dbus","status":"active"},{"name":"grub-common","status":"active"},{"name":"irqbalance","status":"active"},{"name":"kmod","status":"active"},{"name":"lightdm","status":"active"},{"name":"lvm2-lvmetad","status":"active"},{"name":"lvm2-lvmpolld","status":"active"},{"name":"apache-htcacheclean","status":"active"},{"name":"network-manager","status":"active"},{"name":"networking","status":"active"},{"name":"postfix","status":"active"},{"name":"procps","status":"active"},{"name":"resolvconf","status":"active"},{"name":"rsyslog","status":"active"},{"name":"smartmontools","status":"active"},{"name":"smbd","status":"active"},{"name":"speech-dispatcher","status":"active"},{"name":"ssh","status":"active"},{"name":"thermald","status":"active"},{"name":"tlp","status":"active"},{"name":"udev","status":"active"},{"name":"ufw","status":"active"},{"name":"lvm2-lvmpolld","status":"inactive"},{"name":"mysql","status":"active"},{"name":"alsa-utils","status":"inactive"},{"name":"x11-common","status":"inactive"},{"name":"apache-htcacheclean","status":"inactive"},{"name":"avahi-daemon","status":"inactive"},{"name":"console-setup.sh","status":"inactive"},{"name":"cryptdisks","status":"inactive"},{"name":"cryptdisks-early","status":"inactive"},{"name":"cups-browsed","status":"inactive"},{"name":"grub-common","status":"inactive"},{"name":"hwclock.sh","status":"inactive"},{"name":"kerneloops","status":"inactive"},{"name":"keyboard-setup.sh","status":"inactive"},{"name":"laptop-mode","status":"inactive"},{"name":"lvm2","status":"inactive"},{"name":"lvm2-lvmetad","status":"inactive"},{"name":"anacron","status":"inactive"},{"name":"network-manager","status":"inactive"},{"name":"nmbd","status":"inactive"},{"name":"plymouth","status":"inactive"},{"name":"plymouth-log","status":"inactive"},{"name":"pppd-dns","status":"inactive"},{"name":"prometheus","status":"inactive"},{"name":"rsync","status":"inactive"},{"name":"samba","status":"inactive"},{"name":"samba-ad-dc","status":"inactive"},{"name":"saned","status":"inactive"},{"name":"speech-dispatcher","status":"inactive"},{"name":"unattended-upgrades","status":"inactive"},{"name":"uuidd","status":"inactive"},{"name":"unattended-upgrades","status":"active"}]; array = array.sort(function (a,b) { return a.name – b.name }) console.log(array.map(function(i){ return i.name })) 这是输出: 我在nodejs中运行这个代码 [ 'whoopsie', 'unattended-upgrades', 'apache2', 'apparmor', 'apport', 'avahi-daemon', 'bluetooth', 'cron', 'cups', 'cups-browsed', 'dbus', 'grub-common', 'irqbalance', 'kmod', 'lightdm', 'lvm2-lvmetad', 'lvm2-lvmpolld', 'apache-htcacheclean', 'network-manager', 'networking', 'postfix', 'procps', […]

我如何从全局函数声明一个数组?

我想从全局函数声明一个数组,以便我的其他函数也可以使用它,但我不知道如何,因为我使用了一个csvtojson转换器,使整个事情很长,并想知道如果这是申报还是不申请? JS: //require the csvtojson converter class var Converter = require("csvtojson").Converter; // create a new converter object var converter = new Converter({}); var MongoClient = require('mongodb').MongoClient; var url = 'mongodb://localhost:27017/myproject'; // call the fromFile function which takes in the path to your // csv file as well as a callback function var JSarray = converter.fromFile("./NTA-SAM-Inventory-List-Security- […]

在nodejs中添加mongoose对象到数组

我有一个这样的图像arrays。 var newImageParams = [ { image_string: "hello", _type: "NORMAL" }, { image_string: "hello", _type: "NORMAL" } ] 我使用mongo数据库和在nodejs中,我使用mongoose,一切都很好,我可以将数据添加到mongoose表,但添加每个图像后,我试图将结果对象保存到一个数组,在console.log发生,也是正确的,但是当我返回这个数组的时候,它没有任何东西,它是空的 if (newImageParams) { for (var i = newImageParams.length – 1; i >= 0; i–) { // allImages[i] var newImage = new Image(Object.assign(newImageParams[i], {bike: bike._id})); newImage.save(function(err, image){ console.log(image); allImages.push(image); console.log(allImages); }); } } 这就是我正在做的事情,一切都是正确的,在控制台好,但当我最后回来, res.json({bike: bike, images: […]

在Node.JS中一次读取大量文件N行

我有一个65,000,000行的文件,大小约2GB。 我想一次读取N行这个文件,执行db插入操作,然后读取下一个N,在这种情况下N是1000。 插入顺序并不重要,所以同步是好的。 这样做的最好方法是什么? 我只发现一次加载1行,或者将整个文件读入内存的方法。 下面的示例代码,我一直用它读取一行文件。 : var singleFileParser = (file, insertIntoDB) => { var lr = new LineByLineReader(file); lr.on('error', function(err) { // 'err' contains error object console.error(err); console.error("Error reading file!"); }); lr.on('line', function(line) { insertIntoDB(line); // 'line' contains the current line without the trailing newline character. }); lr.on('end', function() { // All lines are […]

在帕格(翡翠)和Node.js中迭代两个数组

我有一个简单的哈巴狗布局,需要一个图像源url的数组和一个相应的网页url的数组,我想同时迭代两个。 基本上我想这样做: for ( i = 0; i < array.length; i++ ) { // display photos[i] // display webLinks[i] } 我正在尝试各种各样的东西,比如说 block content h1= title ul each val, link in photos, webLinks a(href=link) img(src=val width=200 height=150) 但是这似乎只能遍历照片数组。 我试过其他的东西 each val in photos each link in webLinks // rest of code 这给出了一个错误,说它不希望换行符。 我可以通过帕格一个单一的对象组成的这些数组,如果这会更容易。 我没有看到解决这个问题的帕格迭代文档中的任何东西。

NodeJS – 从数组中减去数组,不删除所有重复项

标题可能没有多大意义,但是你将如何做这样的事情: a = [1, 2, 2, 3, 3, 3]; b = [1, 2, 3]; a.subtract(b); 我想这个返回[2,3,3],而不是[]类似的问题的其他答案会,只保留不在其他数组中的项目,而不是只删除有多less其他arrays。

NodeJS:array.join的结果不正确

我注意到array.join与包含(\ r)字符的string数组的不正确的输出。 这是我的代码和输出: var list = [ "one\r", "two\r", "three\r", "four\r"] console.log(list); // ok, output: [ 'one\r', 'two\r', 'three\r', 'four\r' ] console.log(list.join(',')); // incorrect, outputs: ,foure 它在Chrome / Firefox的控制台中工作正常,只是不在nodejs中。 如果有问题,我在linux mint 18.3上使用节点6.11.3 我可以为此做一些解决方法,但是我更关心为什么会发生这种情况。

数组filter,自定义需求和数据操作

const data = [{ employee: 70, month: 0, year: 2017, id: 3, createdAt: '2017-09-15T09:42:37.000Z', updatedAt: '2017-09-15T09:42:37.000Z', organization: 41, version: 1 }, { employee: 70, month: 4, year: 2017, id: 4, createdAt: '2017-09-15T09:59:28.000Z', updatedAt: '2017-09-15T09:59:28.000Z', organization: 41, version: 2 }, { employee: 70, month: 4, year: 2017, id: 5, createdAt: '2017-09-15T10:00:35.000Z', updatedAt: '2017-09-15T10:00:35.000Z', organization: 41, version: […]

通过nodejs传递Mysql查询中的数组

我有一个简单的查询,我想传递其中有5个项目的数组。 我正在使用mysql模块,所以我知道它可以做,但没有做synatx的权利,因此得到一个语法错误。 以下是查询: `UPDATE table1 SET table1.col=0 WHERE (table1.col2) IN = (?) AND table1.id=(SELECT …);`,[arr] //arr = [1,2,3,4,5]; 我努力了: `UPDATE table1 SET table1.col=0 WHERE (table1.col2) IN = (?,?,?,?,?) AND table1.id=(SELECT …);`,[arr]` 但我仍然得到一个语法错误。

组数组对象

我有一个像这样的对象的数组: [{recordID:'123'},{recordID:'123-opp'},{recordsID:'456'},{recordID:'456-opp'},{recordID:'789'}, {recordID:'980'},…] 所以有一些对象具有相同的recordID只是添加了一个-opp ,有些是单一的,没有-opp我想要做的是将相应的对象组合在一个数组中。 另一个单独在一个arrays中。 例如: [[{recordID:'123'},{recordID:'123-opp'}], [{recordID:'456'},{recordID:'456-opp'}],[{recordID:'789'}],[{recordID:'980'}],…] 提一下:有时候可能会在结尾处有-opp ,有时候-ref 。 我尝试了多种方式,但没有得到正确的结果。 这是我的代码: .then(function(dbResult){ //sort and filter the corresponding records var records = []; var indexToDelete; dbResult.forEach(function(item, index){ var recordPair = dbResult.filter(function(element, i){ if(element.recordId.includes(item.recordId)){ indexToDelete = i; return true; }else{ return false; } }) records.splice(indexToDelete,1); records.push(recordPair); }) console.log(records) return records; }) 也许有人有更好的解决scheme。