Tag: 数组

如何合并唯一的两个JSON文件?

我想合并两个包含电影信息的JSON文件。 这些文件有一些共同的项目。 我想写第三个文件,其中包括所有的电影,而不重复他们。 以下是我到目前为止: const fs = require('fs'); const path = require('path'); const readMovies1 = () => { return new Promise((resolve, reject) => { fs.readFile(path.join(__dirname,'../models/movies.json'), 'utf8', (err, data1) => { if(err) reject(err); let data = JSON.parse(data1); resolve(data); }); }); }; const readMovies2 = (data1) => { return new Promise((resolve, reject) =>{ fs.readFile(path.join(__dirname,'../models/movies2.json'), 'utf8', (err, data) […]

通过嵌套的JSON数组对象nodejsparsing

我试图parsing一个JSON对象,并删除其中的密钥ID,我能够删除出现在对象的根目录,但无法遍历的嵌套数组对象,也有' id'键并删除它们,现在跟在代码块之后 var json = { "id" : "a28b469b-b4f2-4846-9b5f-9d866f249bbe", "description" : "Cost of Product", "periodicity" : "calendar-monthly", "Vanilla" : [ { "id" : "22382c50-f56f-40b7-a308-203da052c5bc", "price" : { "amount" : 100.000, "currency" : "USD" }, "packing" : "RECURRING", "billedInAdvance" : true } ], "Chocolate" : [ { "id" : "44672921-1966-456e-bde2-87ef72f31cab", "price" : { "amount" : 256.000000, […]

在JS中创buildHTML元素(没有jQuery)? 我究竟做错了什么?

我是node.js的新手 这是我的代码: 我的代码 更具体地说,这里是我所拥有的JS: let torontoTeams = [ {"name": "Raptors", "description": "The Raptors compete in the National Basketball Association (NBA), as a member club of the league's Eastern Conference Atlantic Division."}, {"name": "Maple Leafs", "description": "The Maple Leafs are one of the 'Original Six' NHL teams, and have won the Stanley Cup 13 times."}, {"name": […]

如何深入比较无序数组?

我想比较两个不必要的数组。 我试图使用ImmutableJS(fromJS),但它不工作与一个无序的数组。 请看看必须相等的示例数组 第一arrays [ { Jnam: 'processor', Ci: 'New York', }, { Jnam: 'keyboard', Ci: 'Washington', }, { Jnam: 'display', Ci: 'Seattle', }, ] 第二 [ { Jnam: 'keyboard', Ci: 'Washington', }, { Jnam: 'processor', Ci: 'New York', }, { Ci: 'Seattle', Jnam: 'display', }, ] 你能回答我在这种情况下做什么吗?

数组中的特征项

我有一个包含在variables“foundListings.currentimages”下面的图像链接数组。 在前端,用户可以检查“特色”框以select要显示的图像。 所有这一切运作良好。 但是,我不知道如何将foundListings.currentimages下的图像移动到数组的前面。 (如果它在arrays的前面,它的特色。) 我将如何更改代码foundListings.currentimages.splice(index2, 1); 不从数组中删除项目,而是把它放在数组中? 谢谢你的帮助! 🙂 // SELECTING FEATURED IMAGE // if any images have been selected for feature, — add it to front of array if(req.body.feature && req.body.feature.length) { for(var i = 0; i < req.body.feature.length; i++) { var index2 = foundListings.currentimages.indexOf(req.body.feature[i]); foundListings.currentimages.splice(index2, 1); } } foundListings.save(); }

通过地图Javascript分析

我通过对从服务器检索的json数据执行某些操作返回了一个映射。 当我打印键和值时,地图结构如下所示 action = [object Map] comedy = [object Map] thriller = [object Map] 而在一个对象地图(即当我只是打印的价值观),他们有我所期望的计数。 Map { 'spanish' => 3 'english' => 4 } Map { 'spanish' => 1 'english' => 2 } 我想转换基于这样的数组内嵌套的地图。 有没有现有的图书馆,我可以利用? var comedy[] = ['spanish_1', 'english_2'] var action[] =['spanish_3', 'english_4'] 请build议如果这是处理这些数据的理想方式,我发现很难从嵌套的地图中获取数据。

JSON.parse适用于数组为零的元素(node.js)

根据文档JSON.parse将第一个参数作为一个string。 我发现了一个意外的行为: try { const a = JSON.parse([ '{"helloworld": 1}', ]); console.log(a); } catch (ex) { console.error(ex); } 我期望它失败,因为提供的input参数是一个数组。 相比之下, JSON.parse成功parsing了数组[0]元素并将其输出(在node.js中)。 但是,如果您将数组传递给两个元素,那么JSON.parse将会出错 try { const b = JSON.parse([ '{"hello": 1}', '{"hello2": 2}', ]); console.log(b); } catch (ex) { console.error(ex); } 为什么?

MongoDB / NodeJS:循环查询参数

我需要你的帮助,因为通过查询各种集合 (谢谢@你的帮助!),我想循环查询的参数,这是非常令人沮丧的,因为,再一次,我不知道发生了什么… 我有这个代码: var mongodb = require("mongodb"); let pairs = ["ETH/EUR", "BTC/EUR", "LTC/EUR", "BCH/EUR"]; mongodb.connect("mongodb://localhost:27017/orderInfo", function(err, db) { if (err) { throw err; } db.listCollections().toArray((err, cols) => { if (err) { throw err; } cols.forEach(col => { pairs.forEach(p => { db .collection(col.name) .findOne({ pair: "BTC/EUR" }, {}, function(err, docs) { if (err) { throw err; […]

将文件推入mongoose模型arrays中的子文件

在我的公司架构中,我有一个张贴的作业是types数组,并将保存子文档 companySchema.js PostedJobs : [{ JobName : { type: String, required : true}, JobType : { type: String, required : true}, JobLocation : { type: String, required : true}, JobSalay: { type: String, required : true} }], 在我/公司路线中,我通过模型中的Creator实体获得由特定用户注册的所有公司 得到我使用的用户公司 router.get('/', isLoggedIn , function(req, res, next) { Company.find({'Creator': req.user.id}).then(function(companies) { res.render('Company', { "Companies" : companies }); […]

如何插入与对象值的数组到MySQL使用for或foreach在JavaScript(nodejs)?

这是我的cron工作api: var job = new cronJob('* * * * * *', function () { Draft.find().then(data => { var finalData = data; Profsms.create({ phoneno: draftData.contacts.gsm, sender: senderName, message: message }).then(function (data) { if (data) { console.log("successfully moved in profsms mysql"); } else { console.log("failed"); } }) }); }, function () { console.log('DelCron Job finished.'); }, true, […]