What is the best way to extract information from a large dataset?

I have a wide range of data stored in the Data_Array below. How can I extract only the 5th and 6th indexes of the data automatically?

var Data_Array = ["BETA 135 MEMB 3 6",
              "MATERIAL STEELAPPROX ALL",
              "SUPPORTS",
              "5 13 16 22 24 PINNED",
              "20 FIXED",
              "7 FIXED BUT FX KFY 200",
              "9 FIXED BUT FZ MX KFY 150 KMZ 200",
              "LOAD 1 LOADTYPE Dead  TITLE DEAD",
              "SELFWEIGHT Y -1",
              "LOAD 2 LOADTYPE Live  TITLE LIVE"]

I am aiming to extract the following values:

["7 FIXED BUT FX KFY 200",
 "9 FIXED BUT FZ MX KFY 150 KMZ 200"]

I am attempting to write a code that iterates through all the arrays and stops when it encounters the word 'FIXED'. It then returns the length of that array as the first index. It continues to count until it reaches 'Load' as the second index. Here is an example code:

function countIndex(array, str1, str2){

  count until Fixed then = gives 5
  count until Load then = give 7

}

Array.splice(1st_index,2nd_index);

Although I have code that accomplishes this task, it only works when the specified strings are in the first index of an element. If the string "FIXED" is not in the first index, the code fails to detect it. Here is the existing code snippet:

function pullAllDataBetween(data, str1, str2) {

  var string_nodes = [];
  var append = false;

  for (var i = 0; i < data.length; i++) {
   if (data[i] === str1) {
      append = true;
      continue;
   } else if (data[i] === str2) {
      append = false;
      break;
   }

      if (append) {
     string_nodes.push(data[i]);
   }
  }

    return string_nodes;
  }

Answer №1

console.log(Data_Array.slice(5, 7)); 

OUTPUT:

[ '7 FIXED BUT FX KFY 200',
  '9 FIXED BUT FZ MX KFY 150 KMZ 200' ]

var Data_Array = ["BETA 135 MEMB 3 6",
          "MATERIAL STEELAPPROX ALL",
          "SUPPORTS",
          "5 13 16 22 24 PINNED",
          "20 FIXED",
          "7 FIXED BUT FX KFY 200",
          "9 FIXED BUT FZ MX KFY 150 KMZ 200",
          "LOAD 1 LOADTYPE Dead  TITLE DEAD",
          "SELFWEIGHT Y -1",
          "LOAD 2 LOADTYPE Live  TITLE LIVE"];

console.log(Data_Array.slice(5, 7)); 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

To access only the strings containing the word FIXED in the array and determine its length:

var Data_Array = ["BETA 135 MEMB 3 6",
          "MATERIAL STEELAPPROX ALL",
          "SUPPORTS",
          "5 13 16 22 24 PINNED",
          "20 FIXED",
          "7 FIXED BUT FX KFY 200",
          "9 FIXED BUT FZ MX KFY 150 KMZ 200",
          "LOAD 1 LOADTYPE Dead  TITLE DEAD",
          "SELFWEIGHT Y -1",
          "LOAD 2 LOADTYPE Live  TITLE LIVE"];


var arr = Data_Array.filter(line => /FIXED/.test(line));

console.log(arr);

console.log(arr.length);

function count(arr, str){
  for(var i = 0; i<arr.length;i++){
    if(arr[i].indexOf(str)!==-1)
      return i;
  }
}

console.log(count(Data_Array, "FIXED"));

console.log(count(Data_Array, "LOAD"));

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Enabling a variable to encompass various types within ReactJS

In my code, I have defined two distinct types as follows: type A = $ReadOnly<{ a: ?$ReadOnlyArray<string>, b: ?string, }>; and type B = $ReadOnly<{ c: ?$ReadOnlyArray<boolean>, d: ?number, }>; Now, I am looking to create a ...

React error due to setting div scroll height on component mount

In my code, there is a special div/container that contains multiple <p/> tags. The container has a reference called divRef, which is initially set to null using the useRef function. <div ref={divRef} style={styles.messageListContainer}> ...

What is the best approach for accessing values from dynamic or multiple form fields upon submission in React?

In my form, users have the ability to add Textfields in order to include additional items. However, I am facing a challenge when it comes to retrieving these items from the form upon submission. The Textfields are dynamically created by a component functi ...

Exploring the power of async/await and promise in TypeScript

I'm puzzled as to why the return type string in this method is showing up as a red error: exportPageAsText(pageNumber: number): string { (async () => { const text = await this.pdfViewerService.getPageAsText(pageNumber); ...

Issue with Node.js and Express redirect not directing to intended URL

Utilizing the nodejs/express view engine in my application allows me to render an assigned template onto the screen when the route points to an existent URL. Previously, I had set up a redirect to the homepage for any user typing in an unexisting URL. Howe ...

The hyperlink element has been added but is not clickable

I'm attempting to incorporate a download feature into my webpage using Greasemonkey. Despite successfully adding the new div element to the page, I am encountering an issue where the download window does not open as expected. var iDiv = document.crea ...

Tips for positioning a modal in the center specifically for zoomed-in mobile devices

I created a modal that uses the following function to center itself: center: function() { var top=Math.max($window.height() - $modal.outerHeight(),0) / 2; var left=Math.max($window.width() - $modal.outerWidth(),0) / 2; $modal.css({ ...

Iterate through a JavaScript array to access objects with varying IDs

Struggling to navigate the JSON data due to ID 29450 and 3000 out of a total of 1500 IDs in the database. Looking to log the information ['Id', 'Description', 'StartDate'] for both IDs. Feeling a bit stuck, hoping someone can ...

Youtube video embedding showing cut edges on smaller screens

Looking for assistance with a Next.js and tailwind site I am building. Having trouble getting the video component to display properly on mobile screens. Tried various fixes but the video still gets cut off on smaller screen sizes. If anyone has a soluti ...

"Downloading an image from an encoded base64 canvas triggers an error due to exceeding the maxUrlLength

I'm using javascript to create a canvas drawing on my webpage and then convert it into a base64 image format. After that, I encode the URL of the image to obtain an encoded URL. I came across a post where it was mentioned that using window.location.hr ...

I am hoping for the outcome to be directed to the homepage

I'm struggling to figure this out, as I am new to classic ASP and JavaScript. I hope someone can help me understand. I want to display the response.write on the main.asp (or the result) page, but each time I try, it redirects to pass.asp on a differen ...

What is the best way to implement a scroll to top/bottom button globally in Vue?

I have created a scroll-to-top and scroll-to-bottom button for my application. However, I want to make these buttons accessible globally throughout the app without having to repeat the code in every page where the component is needed. Currently, I am inclu ...

Utilizing the power of Angular's $resource within a factory to showcase information stored in a deeply nested

Just starting to explore the world of $resource concepts and facing a challenge with a piece of code that involves fetching data from a nested JSON. I've created a factory method that successfully retrieves the data and I can see it in the response he ...

Lack of Ajax query string parameters

To implement a functionality where clicking on a delete button in the UI triggers a service method that retrieves data from a database and displays it in a popup, I need to call a specific method in an Action class. The task id and command name (method nam ...

Learn how to mock asynchronous calls in JavaScript unit testing using Jest

I recently transitioned from Java to TypeScript and am trying to find the equivalent of java junit(Mockito) in TypeScript. In junit, we can define the behavior of dependencies and return responses based on test case demands. Is there a similar way to do t ...

Tips for adjusting the border color of a MUI Select field

https://i.stack.imgur.com/rQOdg.png This MUI select box changes color from blue to black based on selection. The challenge is to customize the text and border color to white (currently set as blue). Any suggestions on how to achieve this? ...

Sending target information as a property argument

I have encountered an issue with my app's two Bootstrap modals. It seems that many people are facing problems with opening the second modal. Is it possible to pass data-target and modal id properties as props in this manner: data-target={props.da ...

Searching for a document using the $eq operator in MongoDB within the context of Next.js - what is

In my Next.js code, I am fetching a document from MongoDB using a unique slug. Here is the code snippet: export async function getStaticProps(context) { const postSlug = context.params.postPage; const { db } = await connectToDatabase(); const posts ...

Expand the div width to the specified measurement horizontally

Is there a way to horizontally collapse a div (container) to a specific width that I can adjust, effectively hiding its content? The collapse effect should move towards the left. <div id="container"> <button type="button" id="myButton"> ...

Cease the use of bi-directional data binding on an Angular directive

One way I've been attempting to send information to a directive is through the following setup: <ui-message data="{{app}}"></ui-message> In my controller, this is how I define it: app.controller("testCtrl", function($scope) { $scope.a ...