简体   繁体   English

搜索相关的json数据

[英]Search for a related json data

How can i find data that is related to the already known data? 如何查找与已知数据相关的数据? ( I'm a newb. ) (我是新手。)

For example here is my json : 例如,这是我的json:

[ 
{ "id": "1",  "log": "1","pass":  "1111" },
{ "id": 2,  "log": "2","pass":  "2222" },
{ "id": 3, "log": "3","pass":  "3333" }
]

Now i know that "log" is 1 and i want to find out the data "pass" that is related to it. 现在我知道"log"1 ,我想找出与它相关的数据"pass"

i've tried to do it so : 我试过这样做:

The POST request comes with log and pass data , i search the .json file for the same log value and if there is the same data then i search for related pass POST请求带有logpass数据,我在.json文件中搜索相同的log值,如果有相同的数据,那么我搜索相关的pass

fs.readFile("file.json", "utf8", function (err, data) {

                var jsonFileArr = []; 
                jsonFileArr = JSON.parse(data);  // Parse .json objekts

                var log = loginData.log; // The 'log' data that comes with POST request

               /* Search through .json file for the same data*/

               var gibtLog = jsonFileArr.some(function (obj) {
                 return obj.log == log;
                });


                if (gotLog) { // If there is the same 'log'

                    var pass = loginData.pass; // The 'pass' data that comes with POST request

                  var gotPass = jsonFileArr.some(function (obj) {
                    // How to change this part ?
                    return obj.pass == pass;
                });

                }
                else
                    console.log("error");

            });

The problem is that when i use 问题在于我何时使用

var gotPass = jsonFileArr.some(function (obj) {
                 return obj.pass == pass;
                });

it searches through the whole .json file and not through only one objekt. 它搜索整个.json文件而不是仅通过一个objekt。

Your main problem is that .some() returns a boolean , whether any of the elements match your predicate or not, but not the element itself. 你的主要问题是.some()返回一个布尔值 ,无论是否有任何元素与你的谓词匹配,而不是元素本身。

You want .find() (which will find and return the first element matching the predicate): 你想要.find() (它将找到并返回与谓词匹配的第一个元素):

const myItem = myArray.find(item => item.log === "1"); // the first matching item
console.log(myItem.pass); // "1111"

Note that it is possible for .find() to not find anything, in which case it returns undefined . 请注意, .find()可能找不到任何内容,在这种情况下它返回undefined

The .some() method returns a boolean that just tells you whether there is at least one item in the array that matches the criteria, it doesn't return the matching item(s). .some()方法返回一个布尔值,它只是告诉您数组中是否至少有一个项符合条件,它不返回匹配项。 Try .filter() instead: 请尝试使用.filter()

   var jsonFileArr = JSON.parse(data);

   var log = loginData.log;

   var matchingItems = jsonFileArr.filter(function (obj) {
     return obj.log == log;
   });

   if (matchingItems.length > 0) {     // Was at least 1 found?
     var pass = matchingItems[0].pass; // The 'pass' data that comes with the first match
   } else
     console.log("error");             // no matches

Using ES6 Array#find is probably the easiest, but you could also do (among other things) 使用ES6 Array#find可能是最简单的,但您也可以(除其他外)

 const x = [{ "id": "1", "log": "1", "pass": "1111" }, { "id": 2, "log": "2", "pass": "2222" }, { "id": 3, "log": "3", "pass": "3333" }]; let myItem; for (let item of x) { if (item.log === '1') { myItem = item; break; } } console.log(myItem); 

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

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