Boninseg
Boninseg

Reputation: 71

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 :

[ 
{ "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.

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

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.

Upvotes: 4

Views: 84

Answers (3)

Xotic750
Xotic750

Reputation: 23492

Using ES6 Array#find is probably the easiest, but you could also do (among other things)

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);

Upvotes: 1

nnnnnn
nnnnnn

Reputation: 150080

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). Try .filter() instead:

   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

Upvotes: 2

Madara's Ghost
Madara's Ghost

Reputation: 175028

Your main problem is that .some() returns a boolean, whether any of the elements match your predicate or not, but not the element itself.

You want .find() (which will find and return the first element matching the predicate):

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.

Upvotes: 3

Related Questions