简体   繁体   English

如何创建一个嵌套循环,以在梦Night javascript代码中输入数据

[英]How can I create a Nested For Loop to enter data in my Nightmare javascript code

I have two JSON files. 我有两个JSON文件。 I want to compare the JSON objects in both files; 我想比较两个文件中的JSON对象; json1 and json2 . json1和json2 If there is an object in the array in the first json file (json1) file that is not present in second json file (json2) , I would like to pass it through the below Nightmare js code then push it the object to the second json file using .push() . 如果第一个json文件(json1)文件中的数组中存在第二个json文件(json2)中不存在的对象 ,我想将其通过下面的Nightmare js代码传递,然后将其推入第二个json中使用.push()文件。


JS CODE: JS代码:

 var Nightmare = require('nightmare'); var nightmare = Nightmare({ show: true }); var json1 = require('./json1.json') var json2 = require('./json2.json') for (var i = 0; i < json1.length; i++) { for (var c = 0; c < json2.length; c++) { if (json1[i] !== (json2[c])) { console.log(json1[i]) return nightmare .goto(json1[0].searchOn) .insert('.gLFyf', json1[0].searchText) .wait(3000) .end() .evaluate((json2, json1) => { return json2[c].push(json1[i]) }, json2, json1) .then() } else { console.log('End!') } } } 

JSON1 Data JSON1资料

 [ { "searchOn": "https://www.google.com", "searchText": "I love google" }, { "searchOn": "https://www.google.com", "searchText": "I'm hungry, where can I eat?'" } ] 

JSON2 Data JSON2资料

 [ { "searchOn": "https://www.google.com", "searchText": "What's the date?" }, { "searchOn": "https://www.google.com", "searchText": "What is the internet" }, { "searchOn": "https://www.google.com", "searchText": "What's the weather like today?" } ] 

However, the Js code gives me this error: UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'push' of undefined . 但是,Js代码给了我这个错误: UnhandledPromiseRejectionWarning:TypeError:无法读取undefined的属性“ push”

The code also does not execute the nightmare js code on all objects on the loop (only performs the task for the first item alone. 该代码也不会在循环上的所有对象上执行噩梦js代码(仅对第一项执行任务。

Please advice on how I can fix these errors. 请提供有关如何解决这些错误的建议。

Kind regards. 亲切的问候。

You can't iterate through an object by indexes. 您不能通过索引遍历对象。 This would work with arrays, which are a type of object with keys mapped to indexes starting at 0. If you want to iterate through any object use Object.keys or for-in operator. 这将适用于数组,数组是一种对象,其键映射到从0开始的索引。如果要遍历任何对象,请使用Object.keys或for-in运算符。 You can find out how to do both here . 您可以在此处了解如何进行这两项操作。

The same is true about push, which is an array method 对于推( 数组方法)也是如此

The interaction between multiple code errors can be difficult to predict and/or explain. 多个代码错误之间的交互可能难以预测和/或解释。 Here's a few to review: 这里有一些评论:

  1. if (json1[i] !== (json2[c]))

The entries in json1 and json2 are objects created by JSON.parse . json1json2中的条目是JSON.parse创建的对象。 They are always different objects. 它们始终是不同的对象。 Comparing equality of their primitive property values will be needed to see if the objects contain the same data. 需要比较它们的原始属性值的相等性,以查看对象是否包含相同的数据。

  1. return nightmare ... return nightmare ...

This would synchronously return the pending promise returned by then() at the end of the promise chain from the function in which this code is executing, so only the first loop iteration is performed. 这将在执行此代码的函数中,在promise链的末尾同步返回then()返回的待处理的promise,因此仅执行第一次循环迭代。 The return keyword needs to be removed for the loop to complete. 需要删除return关键字才能完成循环。

  1. json2[c].push(json1[i] in one of the call backs json2[c].push(json1[i]在回叫之一

has two problems: 有两个问题:

a) it is executed asynchronously. a)它是异步执行的。 If the return is removed and the loop completes, c and i are equal to the length of json2 and json respectively and return undefined if used as array indices. 如果return被删除并且循环完成,则ci分别等于json2json的长度,如果用作数组索引,则返回undefined。 This is an asynchronous programming issue - refer to JavaScript closure inside loops – simple practical example for solutions. 这是一个异步编程问题-引用循环内的JavaScript闭合-解决方案的简单实际示例

b) json2[c] is a plain object, not an array, so it doesn't have a push method. b) json2[c]是一个普通对象,而不是数组,因此它没有push方法。 You probably meant to push a value on the end of json2 , not onto an entry in it. 您可能打算将值推入json2的末尾,而不是推入其中的条目。

  1. There is no catch clause on the nightmare promise chain. 梦m承诺链上没有catch子句。 Uncaught promise rejection errors may become fatal in the future. 未捕获的承诺拒绝错误将来可能会致命。

Note I have no exact reason why json2[c] is undefined in the code snippet - are you quoting from actual code rather than the cut down example posted? 注意我没有确切的理由为什么在代码片段中未定义json2[c] -您是引用实际代码,而不是发布的缩减示例吗? I would also suggest looking into asynchronous functions and the await operator as a means of looping asynchronous operations. 我还建议研究异步函数和await运算符,作为循环异步操作的一种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM