Tag: 数组

NodeJs的Readline值放入数组中

我正在使用NodeJS readline模块从文本文件中读取。 它将每行都返回到控制台,但是我希望它将返回的行添加到数组。 这是我到目前为止的代码,但它不工作。 我认为rd.on是一个for循环,但它看起来不像。 任何帮助,将不胜感激! function getinfomation2(){ var firstfile = []; var secondfile = []; var countvar = 0; readline = require('readline'); var rd = readline.createInterface({ input: fs.createReadStream('../FixtureProfiles/CFIP1.json'), output: process.stdout, terminal: false }); // Add fixture details from file to array to later be called rd.on('line', function(line) { // if fails console.log(line) returns line […]

检查两个数组中的JSON属性

介绍: 我正在使用d3.js库来绘制一个力布局图。 为了让图的更新过程看起来很平常,我想检查一个名为nodes的数组,它包含图中的所有当前nodes和新的incoming json object如果它们具有基于每个node具有的name属性的共享元素)。 示例JSON: { "nodes":[ {"name":"Harry Potter", "shortname":"Harry", "id":0}, {"name":"Severus Snape", "shortname":"Severus", "id":1} ], "links":[ {"source":0,"target":1,"relation":"hasTeacher"} ] } 每个name都是唯一的( 是的,我知道你们中的一些人会争论名字不应该是唯一的原因 ),这个object将是一个函数的input参数。 function: 以下function将所有新的节点推到图上。 function pushNewElements(json) { var len = json.nodes.length; var difference = json.nodes.filter(function (el) { return isInGraph(el, len); }); difference.forEach(function (node) { nodes.push(node); }); } filter()函数 这个函数应该像filter一样工作,并获得nodes和json.nodes数组之间的json.nodes 。 经过多次testing,这是我能想到的最好的。 function isInGraph(jnode, […]

将两个对象与数组合并在一起

我试图合并两个JavaScript对象,其中有数组和其他元素。 让我举一个例子。 { "addresses": [ { "type": "email", "tags": [ "Responsável", "Pai" ], "address": "johndoepai1@gmail.com" }, { "type": "phone", "tags": [ "Responsável", "Mãe" ], "address": "551138839332" }, { "type": "email", "tags": [ "Mãe" ], "address": "johndoemae1@gmail.com" }, { "type": "email", "tags": [ "Aluno" ], "address": "johndoealuno1@gmail.com" } ], "class": [ "Sala 1", "Sala 2", "Sala […]

对于每个元素,单击更改值

我已经使用nodejs处理栏为每个表创build一个: <tbody> {{#each result}} <tr> <td id="engagementTotal">{{data.engagementTotal}}</td> </tr> {{/each}} </tbody> 但我需要通过onclick函数来更改每个值的值,我试图使用 for (var i=0; i < positiveTotal.length; i++){ sEngagementTotal.push(positiveTotal[i].data.engagementTotal); } document.getElementById("engagementTotal").innerHTML = sEngagementTotal; 但是上面只改变了第一个表的值

从mongoDB集合中删除数组项目

我正在努力做下面的事情,如果有人能帮上忙,那将是很大的帮助 我想从items数组中删除一个项目 { "_id" : ObjectId("56e8c56a71cfa3fbe528c91b"), "shipping" : 6.7800000000000002, "subTotal" : 201, "totalCost" : 207.7800000000000011, "order_status" : "queued", "ordered_by" : "John Sims", "ordered_by_id" : "56e8c50a71cfa3fbe5288a48", "created_at" : ISODate("2016-03-16T02:31:06.723Z"), "items" : [ { "created_at" : "2008-12-17T11:01:23.460Z", "quantity" : 1, "country" : "Australia", "state" : "NSW", "suburb" : "Sydney", "location" : "Sydney,NSW,Australia", "longitude" : "151.1228434", "latitude" : […]

MongoDB-通过比较集合与对象数组返回现有字段的数组

我是新来的Mongo,并试图比较一个数组与收集文件和匹配logging的返回列表。 让我解释一下: First Array 我有一个收集(用户)与以下文件: > db.user.find().pretty() { "_id" : ObjectId("57358220bf3e7d076b6ccdb1"), "name" : "Sunny", "phone" : "9417702107", "email" : "ssdhiman07@gmail.com" } { "_id" : ObjectId("57358242bf3e7d076b6ccdb2"), "name" : "Pal", "phone" : "9015719419", "email" : "ss998@gmail.com" } { "_id" : ObjectId("57358262bf3e7d076b6ccdb3"), "name" : "viveky", "phone" : "8826565107", "email" : "sds998@gmail.com" } 第二个数组 :我有一个来自Http请求的对象数组是数组的结构。 { "contacts" : [ […]

NodeJS结合了对象数组的范围属性

标题可能有点混乱,但我会详细解释它。 我有一个用户界面表,用户可以从那里selectdate范围, 星期一 – {in:“13:00:00”,out:“13:59:59”} 星期二 – [{in:“13:00:00”,out:“13:59:59”},{in:“14:00:00”,out:“14:59:59”}] 用户每天只能select多个小时的时间间隔。 我已经根据他们的date进行了分组,并结合间隔 星期二 – [{in:“13:00:00”,out:“14:59:59”},{in:“14:00:00”,out:“14:59:59”}] 在第一次迭代。 但我不知道如何使它超过4或5小时的时间间隔.FYI我使用lodash进行sorting和分组,以及将小时转换为int的时刻。 如果用户input5个星期二的时间间隔,例如[{in:“13:00:00”,out:“13:59:59”},{in:“14:00:00”,out:“14:59:59 “},{在:” 15:00:00" ,指出: “15点59分59秒”},{在: “18:00:00”,指出: “18时59分59秒”},{在: “19:00:00”,指出: “十九时59分59秒”}] 我想要范围结合像; [{在: “13:00:00”,指出: “15点59分59秒”},{在: “18:00:00”,指出: “19点59分59秒”}] 任何帮助或build议将不胜感激。

成功search的执行时间二进制searchalgorithm

我已经实现了使用Node.js的二进制searchalgorithm。 我logging了algorithm在随机生成的数组中search数字所用的时间。 我能够输出一个不成功的searchalgorithm所花费的时间。 但我无法弄清楚如何测量algorithm成功search数组中的数字所花费的时间。 这是我的代码 – function binarySearch(A,K) { var l = 0; // min var r = A.length – 1; //max var n = A.length; var time = process.hrtime(); while(l <= r) { var m = Math.floor((l + r)/2); if(K == A[m]) { return m; } else if(K < A[m]) { r = m […]

如何从angularjs $ http.post中读取/保存nodeJs中的json文件

厌恶糟糕的英语 我有一个问题,试图抓住一个json使用$ http.post fron一个angularjs发送到一个nodejs express(我必须发送它,因为我不能在客户端保存在服务器上的文件) 我有这个代码在angularjs var app = angular.module('angularTable', []); app.controller('listdata',function($scope, $http){ $scope.users = []; //declare an empty array $http.get("data/people.json").success(function(response){ $scope.users = response; //this works well }); $scope.add = function(){ $scope.users.push({'id':$scope.users.length,'name':$scope.name,'age': $scope.age}); //this works well $http.post("data",$scope.users).success(function(data, status, headers, config){ //this send to the nodeJs the object console.log("success"); }).error(function(data,status,headers,config){ console.log("error"); }); } }); 这在服务器nodeJs中 var […]

如何使用javascript或lodash重新排列对象属性的数组

我有我要写入Excel文件的对象数组。 我有很多像这样的对象的数组 [{'FirstName':'abc','Reg-no':1234,'branch':'mech','ReportDate':'11/12/2014','LastName':'HR'},{'FirstName':'xyz','Reg-no':1235,'branch':'CS','ReportDate':'11/12/2015','LastName':'BC'},{'FirstName':'pqr','Reg-no':1236,'branch':'IS','ReportDate':'11/12/2016','LastName':'TY'}] 我想重新排列这样的对象属性,以便在Excel First列中从注册编号开始 [{'Reg-no':1234,'ReportDate':'11/12/2014','FirstName':'abc','LastName':'HR','branch':'mech'}, {'Reg-no':1235,'ReportDate':'11/12/2015','FirstName':'xyz','LastName':'BC','branch':'CS'}, {'Reg-no':1236,'ReportDate':'11/12/2016','FirstName':'pqr','LastName':'TY','branch':'IS'} ] 有人能帮助我吗?