sorting element of one array into two other arrays by a specific element












2















hi i've got an array i made from a txt file. each element of the array is something like this:



examplea   A   10.20.5.197
exampleb A 10.10.7.178
examplec A 10.20.75.116
exampled A 10.20.90.170


i wanna loop through the original array to find all the ip addresses that are "/10.20.d+.d+/" and store them in another array (wifiArray) and store the rest in another(localArray).
here's what i got so far:



var fs = require('fs');
var lineArray =
fs.readFileSync('C:/Users/intern3/zonerecords.txt').toString().split("n");
var IP = /10.20d+.d+/;

var wifiArray = new Array();
var localArray = new Array();

function ArraySplit(string, Originalarray, Array1, Array2) {
var ind = 0;
for( var i=0; i < Originalarray.length; i++) {

if(String(string).match(Originalarray[i])) {
Array1[ind++] = Originalarray[i];
}
else {
Array2[ind++] = Originalarray[i];
}
}
}

ArraySplit(IP, lineArray, wifiArray, localArray);

fs.writeFile('C:/Users/intern3/test2.txt', wifiArray, function(err) {
if(err) {
return console.log(err);
}

console.log("The file was saved!");
});

fs.writeFile('C:/Users/intern3/test3.txt', localArray, function(err) {
if(err) {
return console.log(err);
}

console.log("The file was saved!");


});

fs.writeFile('C:/Users/intern3/test.txt', lineArray, function(err) {
if(err) {
return console.log(err);
}

console.log("The file was saved!");


});


if i run this test2.txt just prints a wall of ,,,,,,,,,,,,
and test3.txt prints the original array.
what am i doing wrong?










share|improve this question



























    2















    hi i've got an array i made from a txt file. each element of the array is something like this:



    examplea   A   10.20.5.197
    exampleb A 10.10.7.178
    examplec A 10.20.75.116
    exampled A 10.20.90.170


    i wanna loop through the original array to find all the ip addresses that are "/10.20.d+.d+/" and store them in another array (wifiArray) and store the rest in another(localArray).
    here's what i got so far:



    var fs = require('fs');
    var lineArray =
    fs.readFileSync('C:/Users/intern3/zonerecords.txt').toString().split("n");
    var IP = /10.20d+.d+/;

    var wifiArray = new Array();
    var localArray = new Array();

    function ArraySplit(string, Originalarray, Array1, Array2) {
    var ind = 0;
    for( var i=0; i < Originalarray.length; i++) {

    if(String(string).match(Originalarray[i])) {
    Array1[ind++] = Originalarray[i];
    }
    else {
    Array2[ind++] = Originalarray[i];
    }
    }
    }

    ArraySplit(IP, lineArray, wifiArray, localArray);

    fs.writeFile('C:/Users/intern3/test2.txt', wifiArray, function(err) {
    if(err) {
    return console.log(err);
    }

    console.log("The file was saved!");
    });

    fs.writeFile('C:/Users/intern3/test3.txt', localArray, function(err) {
    if(err) {
    return console.log(err);
    }

    console.log("The file was saved!");


    });

    fs.writeFile('C:/Users/intern3/test.txt', lineArray, function(err) {
    if(err) {
    return console.log(err);
    }

    console.log("The file was saved!");


    });


    if i run this test2.txt just prints a wall of ,,,,,,,,,,,,
    and test3.txt prints the original array.
    what am i doing wrong?










    share|improve this question

























      2












      2








      2








      hi i've got an array i made from a txt file. each element of the array is something like this:



      examplea   A   10.20.5.197
      exampleb A 10.10.7.178
      examplec A 10.20.75.116
      exampled A 10.20.90.170


      i wanna loop through the original array to find all the ip addresses that are "/10.20.d+.d+/" and store them in another array (wifiArray) and store the rest in another(localArray).
      here's what i got so far:



      var fs = require('fs');
      var lineArray =
      fs.readFileSync('C:/Users/intern3/zonerecords.txt').toString().split("n");
      var IP = /10.20d+.d+/;

      var wifiArray = new Array();
      var localArray = new Array();

      function ArraySplit(string, Originalarray, Array1, Array2) {
      var ind = 0;
      for( var i=0; i < Originalarray.length; i++) {

      if(String(string).match(Originalarray[i])) {
      Array1[ind++] = Originalarray[i];
      }
      else {
      Array2[ind++] = Originalarray[i];
      }
      }
      }

      ArraySplit(IP, lineArray, wifiArray, localArray);

      fs.writeFile('C:/Users/intern3/test2.txt', wifiArray, function(err) {
      if(err) {
      return console.log(err);
      }

      console.log("The file was saved!");
      });

      fs.writeFile('C:/Users/intern3/test3.txt', localArray, function(err) {
      if(err) {
      return console.log(err);
      }

      console.log("The file was saved!");


      });

      fs.writeFile('C:/Users/intern3/test.txt', lineArray, function(err) {
      if(err) {
      return console.log(err);
      }

      console.log("The file was saved!");


      });


      if i run this test2.txt just prints a wall of ,,,,,,,,,,,,
      and test3.txt prints the original array.
      what am i doing wrong?










      share|improve this question














      hi i've got an array i made from a txt file. each element of the array is something like this:



      examplea   A   10.20.5.197
      exampleb A 10.10.7.178
      examplec A 10.20.75.116
      exampled A 10.20.90.170


      i wanna loop through the original array to find all the ip addresses that are "/10.20.d+.d+/" and store them in another array (wifiArray) and store the rest in another(localArray).
      here's what i got so far:



      var fs = require('fs');
      var lineArray =
      fs.readFileSync('C:/Users/intern3/zonerecords.txt').toString().split("n");
      var IP = /10.20d+.d+/;

      var wifiArray = new Array();
      var localArray = new Array();

      function ArraySplit(string, Originalarray, Array1, Array2) {
      var ind = 0;
      for( var i=0; i < Originalarray.length; i++) {

      if(String(string).match(Originalarray[i])) {
      Array1[ind++] = Originalarray[i];
      }
      else {
      Array2[ind++] = Originalarray[i];
      }
      }
      }

      ArraySplit(IP, lineArray, wifiArray, localArray);

      fs.writeFile('C:/Users/intern3/test2.txt', wifiArray, function(err) {
      if(err) {
      return console.log(err);
      }

      console.log("The file was saved!");
      });

      fs.writeFile('C:/Users/intern3/test3.txt', localArray, function(err) {
      if(err) {
      return console.log(err);
      }

      console.log("The file was saved!");


      });

      fs.writeFile('C:/Users/intern3/test.txt', lineArray, function(err) {
      if(err) {
      return console.log(err);
      }

      console.log("The file was saved!");


      });


      if i run this test2.txt just prints a wall of ,,,,,,,,,,,,
      and test3.txt prints the original array.
      what am i doing wrong?







      javascript arrays node.js






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 13:02









      Adam LockAdam Lock

      111




      111
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Okay, found your problem. You are naming (And using!) your variables incorrectly; And you have incorrect pattern. Try var IP = /10.20.d+.d+/;
          You have:



          function ArraySplit(string, Originalarray, Array1, Array2) {
          // ...

          // This is WRONG, as string is a PATTERN to be matched
          //if(String(string).match(Originalarray[i])) {
          // Now it is correct more or less
          if(String(Originalarray[i]).match(string)) {
          // ...
          }
          }


          always use verbose names. It costs you nothing to have ipPatternMatcher vs pattern or even string



          For the problem for topic, you can use reducer



          const orig = [{
          a: "examplea",
          b: "A",
          ip: "10.20.5.197"
          },
          {
          a: "exampleb",
          b: "A",
          ip: "10.10.7.178"
          },
          {
          a: "examplec",
          b: "A",
          ip: "10.20.75.116"
          },
          {
          a: "exampled",
          b: "A",
          ip: "10.20.90.170"
          }
          ]

          var IPPattern = /^10.20.d{1,3}.d{1,3}$/;

          const accumulator = {wifiAddresses: , localAddresses: };

          const reducer = (accumulator, value) => {
          if (value.ip.match(IPPattern)) {
          accumulator.wifiAddresses.push(value);
          } else {
          accumulator.localAddresses.push(value);
          }
          return accumulator;
          }

          console.log(orig.reduce(reducer, accumulator))


          I'll try to find where you have a problem, but it seems like you have error with regexp and unescaping dot






          share|improve this answer


























          • yes! my regexp pattern was wrong, that was silly of me. thanks for the advice.

            – Adam Lock
            Nov 25 '18 at 13:43











          • Ok i had a chance to look through your solution and it does exactly what i want it to do but the trouble is my txt file has thousands of data, so i can't really write them all in the array individually like that. is there something i can do to make the txt file into json something?

            – Adam Lock
            Nov 26 '18 at 0:40











          • You already have it as an array var lineArray = fs.readFileSync('C:/Users/intern3/zonerecords.txt').toString().split("n");. All you have to do before calling the reduce function is to map these lines into an object, like so: fileArray.map(line => { var temp = line.split("\s+"); return {label:temp[0], type:temp[1], ip:temp[2]}; }).reduce(...

            – Jacek Lipiec
            Nov 26 '18 at 5:15













          • ok cool that split the lines into objects. but now i get a "cannot read property 'match' of undefined"

            – Adam Lock
            Nov 27 '18 at 2:17











          • if (value.ip.match(IPPattern))

            – Adam Lock
            Nov 27 '18 at 2:22











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467713%2fsorting-element-of-one-array-into-two-other-arrays-by-a-specific-element%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Okay, found your problem. You are naming (And using!) your variables incorrectly; And you have incorrect pattern. Try var IP = /10.20.d+.d+/;
          You have:



          function ArraySplit(string, Originalarray, Array1, Array2) {
          // ...

          // This is WRONG, as string is a PATTERN to be matched
          //if(String(string).match(Originalarray[i])) {
          // Now it is correct more or less
          if(String(Originalarray[i]).match(string)) {
          // ...
          }
          }


          always use verbose names. It costs you nothing to have ipPatternMatcher vs pattern or even string



          For the problem for topic, you can use reducer



          const orig = [{
          a: "examplea",
          b: "A",
          ip: "10.20.5.197"
          },
          {
          a: "exampleb",
          b: "A",
          ip: "10.10.7.178"
          },
          {
          a: "examplec",
          b: "A",
          ip: "10.20.75.116"
          },
          {
          a: "exampled",
          b: "A",
          ip: "10.20.90.170"
          }
          ]

          var IPPattern = /^10.20.d{1,3}.d{1,3}$/;

          const accumulator = {wifiAddresses: , localAddresses: };

          const reducer = (accumulator, value) => {
          if (value.ip.match(IPPattern)) {
          accumulator.wifiAddresses.push(value);
          } else {
          accumulator.localAddresses.push(value);
          }
          return accumulator;
          }

          console.log(orig.reduce(reducer, accumulator))


          I'll try to find where you have a problem, but it seems like you have error with regexp and unescaping dot






          share|improve this answer


























          • yes! my regexp pattern was wrong, that was silly of me. thanks for the advice.

            – Adam Lock
            Nov 25 '18 at 13:43











          • Ok i had a chance to look through your solution and it does exactly what i want it to do but the trouble is my txt file has thousands of data, so i can't really write them all in the array individually like that. is there something i can do to make the txt file into json something?

            – Adam Lock
            Nov 26 '18 at 0:40











          • You already have it as an array var lineArray = fs.readFileSync('C:/Users/intern3/zonerecords.txt').toString().split("n");. All you have to do before calling the reduce function is to map these lines into an object, like so: fileArray.map(line => { var temp = line.split("\s+"); return {label:temp[0], type:temp[1], ip:temp[2]}; }).reduce(...

            – Jacek Lipiec
            Nov 26 '18 at 5:15













          • ok cool that split the lines into objects. but now i get a "cannot read property 'match' of undefined"

            – Adam Lock
            Nov 27 '18 at 2:17











          • if (value.ip.match(IPPattern))

            – Adam Lock
            Nov 27 '18 at 2:22
















          1














          Okay, found your problem. You are naming (And using!) your variables incorrectly; And you have incorrect pattern. Try var IP = /10.20.d+.d+/;
          You have:



          function ArraySplit(string, Originalarray, Array1, Array2) {
          // ...

          // This is WRONG, as string is a PATTERN to be matched
          //if(String(string).match(Originalarray[i])) {
          // Now it is correct more or less
          if(String(Originalarray[i]).match(string)) {
          // ...
          }
          }


          always use verbose names. It costs you nothing to have ipPatternMatcher vs pattern or even string



          For the problem for topic, you can use reducer



          const orig = [{
          a: "examplea",
          b: "A",
          ip: "10.20.5.197"
          },
          {
          a: "exampleb",
          b: "A",
          ip: "10.10.7.178"
          },
          {
          a: "examplec",
          b: "A",
          ip: "10.20.75.116"
          },
          {
          a: "exampled",
          b: "A",
          ip: "10.20.90.170"
          }
          ]

          var IPPattern = /^10.20.d{1,3}.d{1,3}$/;

          const accumulator = {wifiAddresses: , localAddresses: };

          const reducer = (accumulator, value) => {
          if (value.ip.match(IPPattern)) {
          accumulator.wifiAddresses.push(value);
          } else {
          accumulator.localAddresses.push(value);
          }
          return accumulator;
          }

          console.log(orig.reduce(reducer, accumulator))


          I'll try to find where you have a problem, but it seems like you have error with regexp and unescaping dot






          share|improve this answer


























          • yes! my regexp pattern was wrong, that was silly of me. thanks for the advice.

            – Adam Lock
            Nov 25 '18 at 13:43











          • Ok i had a chance to look through your solution and it does exactly what i want it to do but the trouble is my txt file has thousands of data, so i can't really write them all in the array individually like that. is there something i can do to make the txt file into json something?

            – Adam Lock
            Nov 26 '18 at 0:40











          • You already have it as an array var lineArray = fs.readFileSync('C:/Users/intern3/zonerecords.txt').toString().split("n");. All you have to do before calling the reduce function is to map these lines into an object, like so: fileArray.map(line => { var temp = line.split("\s+"); return {label:temp[0], type:temp[1], ip:temp[2]}; }).reduce(...

            – Jacek Lipiec
            Nov 26 '18 at 5:15













          • ok cool that split the lines into objects. but now i get a "cannot read property 'match' of undefined"

            – Adam Lock
            Nov 27 '18 at 2:17











          • if (value.ip.match(IPPattern))

            – Adam Lock
            Nov 27 '18 at 2:22














          1












          1








          1







          Okay, found your problem. You are naming (And using!) your variables incorrectly; And you have incorrect pattern. Try var IP = /10.20.d+.d+/;
          You have:



          function ArraySplit(string, Originalarray, Array1, Array2) {
          // ...

          // This is WRONG, as string is a PATTERN to be matched
          //if(String(string).match(Originalarray[i])) {
          // Now it is correct more or less
          if(String(Originalarray[i]).match(string)) {
          // ...
          }
          }


          always use verbose names. It costs you nothing to have ipPatternMatcher vs pattern or even string



          For the problem for topic, you can use reducer



          const orig = [{
          a: "examplea",
          b: "A",
          ip: "10.20.5.197"
          },
          {
          a: "exampleb",
          b: "A",
          ip: "10.10.7.178"
          },
          {
          a: "examplec",
          b: "A",
          ip: "10.20.75.116"
          },
          {
          a: "exampled",
          b: "A",
          ip: "10.20.90.170"
          }
          ]

          var IPPattern = /^10.20.d{1,3}.d{1,3}$/;

          const accumulator = {wifiAddresses: , localAddresses: };

          const reducer = (accumulator, value) => {
          if (value.ip.match(IPPattern)) {
          accumulator.wifiAddresses.push(value);
          } else {
          accumulator.localAddresses.push(value);
          }
          return accumulator;
          }

          console.log(orig.reduce(reducer, accumulator))


          I'll try to find where you have a problem, but it seems like you have error with regexp and unescaping dot






          share|improve this answer















          Okay, found your problem. You are naming (And using!) your variables incorrectly; And you have incorrect pattern. Try var IP = /10.20.d+.d+/;
          You have:



          function ArraySplit(string, Originalarray, Array1, Array2) {
          // ...

          // This is WRONG, as string is a PATTERN to be matched
          //if(String(string).match(Originalarray[i])) {
          // Now it is correct more or less
          if(String(Originalarray[i]).match(string)) {
          // ...
          }
          }


          always use verbose names. It costs you nothing to have ipPatternMatcher vs pattern or even string



          For the problem for topic, you can use reducer



          const orig = [{
          a: "examplea",
          b: "A",
          ip: "10.20.5.197"
          },
          {
          a: "exampleb",
          b: "A",
          ip: "10.10.7.178"
          },
          {
          a: "examplec",
          b: "A",
          ip: "10.20.75.116"
          },
          {
          a: "exampled",
          b: "A",
          ip: "10.20.90.170"
          }
          ]

          var IPPattern = /^10.20.d{1,3}.d{1,3}$/;

          const accumulator = {wifiAddresses: , localAddresses: };

          const reducer = (accumulator, value) => {
          if (value.ip.match(IPPattern)) {
          accumulator.wifiAddresses.push(value);
          } else {
          accumulator.localAddresses.push(value);
          }
          return accumulator;
          }

          console.log(orig.reduce(reducer, accumulator))


          I'll try to find where you have a problem, but it seems like you have error with regexp and unescaping dot







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 25 '18 at 13:26

























          answered Nov 25 '18 at 13:18









          Jacek LipiecJacek Lipiec

          1917




          1917













          • yes! my regexp pattern was wrong, that was silly of me. thanks for the advice.

            – Adam Lock
            Nov 25 '18 at 13:43











          • Ok i had a chance to look through your solution and it does exactly what i want it to do but the trouble is my txt file has thousands of data, so i can't really write them all in the array individually like that. is there something i can do to make the txt file into json something?

            – Adam Lock
            Nov 26 '18 at 0:40











          • You already have it as an array var lineArray = fs.readFileSync('C:/Users/intern3/zonerecords.txt').toString().split("n");. All you have to do before calling the reduce function is to map these lines into an object, like so: fileArray.map(line => { var temp = line.split("\s+"); return {label:temp[0], type:temp[1], ip:temp[2]}; }).reduce(...

            – Jacek Lipiec
            Nov 26 '18 at 5:15













          • ok cool that split the lines into objects. but now i get a "cannot read property 'match' of undefined"

            – Adam Lock
            Nov 27 '18 at 2:17











          • if (value.ip.match(IPPattern))

            – Adam Lock
            Nov 27 '18 at 2:22



















          • yes! my regexp pattern was wrong, that was silly of me. thanks for the advice.

            – Adam Lock
            Nov 25 '18 at 13:43











          • Ok i had a chance to look through your solution and it does exactly what i want it to do but the trouble is my txt file has thousands of data, so i can't really write them all in the array individually like that. is there something i can do to make the txt file into json something?

            – Adam Lock
            Nov 26 '18 at 0:40











          • You already have it as an array var lineArray = fs.readFileSync('C:/Users/intern3/zonerecords.txt').toString().split("n");. All you have to do before calling the reduce function is to map these lines into an object, like so: fileArray.map(line => { var temp = line.split("\s+"); return {label:temp[0], type:temp[1], ip:temp[2]}; }).reduce(...

            – Jacek Lipiec
            Nov 26 '18 at 5:15













          • ok cool that split the lines into objects. but now i get a "cannot read property 'match' of undefined"

            – Adam Lock
            Nov 27 '18 at 2:17











          • if (value.ip.match(IPPattern))

            – Adam Lock
            Nov 27 '18 at 2:22

















          yes! my regexp pattern was wrong, that was silly of me. thanks for the advice.

          – Adam Lock
          Nov 25 '18 at 13:43





          yes! my regexp pattern was wrong, that was silly of me. thanks for the advice.

          – Adam Lock
          Nov 25 '18 at 13:43













          Ok i had a chance to look through your solution and it does exactly what i want it to do but the trouble is my txt file has thousands of data, so i can't really write them all in the array individually like that. is there something i can do to make the txt file into json something?

          – Adam Lock
          Nov 26 '18 at 0:40





          Ok i had a chance to look through your solution and it does exactly what i want it to do but the trouble is my txt file has thousands of data, so i can't really write them all in the array individually like that. is there something i can do to make the txt file into json something?

          – Adam Lock
          Nov 26 '18 at 0:40













          You already have it as an array var lineArray = fs.readFileSync('C:/Users/intern3/zonerecords.txt').toString().split("n");. All you have to do before calling the reduce function is to map these lines into an object, like so: fileArray.map(line => { var temp = line.split("\s+"); return {label:temp[0], type:temp[1], ip:temp[2]}; }).reduce(...

          – Jacek Lipiec
          Nov 26 '18 at 5:15







          You already have it as an array var lineArray = fs.readFileSync('C:/Users/intern3/zonerecords.txt').toString().split("n");. All you have to do before calling the reduce function is to map these lines into an object, like so: fileArray.map(line => { var temp = line.split("\s+"); return {label:temp[0], type:temp[1], ip:temp[2]}; }).reduce(...

          – Jacek Lipiec
          Nov 26 '18 at 5:15















          ok cool that split the lines into objects. but now i get a "cannot read property 'match' of undefined"

          – Adam Lock
          Nov 27 '18 at 2:17





          ok cool that split the lines into objects. but now i get a "cannot read property 'match' of undefined"

          – Adam Lock
          Nov 27 '18 at 2:17













          if (value.ip.match(IPPattern))

          – Adam Lock
          Nov 27 '18 at 2:22





          if (value.ip.match(IPPattern))

          – Adam Lock
          Nov 27 '18 at 2:22




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467713%2fsorting-element-of-one-array-into-two-other-arrays-by-a-specific-element%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Feedback on college project

          Futebolista

          Albești (Vaslui)