Tips for extracting a targeted array from a collection of arrays

When using array.some, I am having trouble getting a single array as an output.

I attempted the following code:

data = [{
    Surname: 'Santos',
    Firstname: 'Carlos'
  },
  {
    Surname: 'Reyes',
    Firstname: 'Carla'
  },
  {
    Surname: 'Michael',
    Firstname: 'Lebowski'
  }
];

var found = data.some(function(data) {
  return data.Surname === 'Reyes'
})

console.log(found);

The logs being returned are:

0: {Surname: 'Santos', Firstname: 'Carlos'}
1 : {Surname: 'Reyes', Firstname: 'Carla'}
2 : {Surname: 'Michael', Firstname: 'Lebowski'}

My expected logs were:

0: {Surname: 'Reyes', Firstname:'Carla'}

How can I modify my code to get the desired output?

Answer №1

Utilize the Array.Filter function to retrieve the specified data based on a condition.

sampleData = [
  { LastName: 'Doe',  FirstName: 'John'   },
  { LastName: 'Smith', FirstName: 'Jane'    },
  { LastName: 'Williams', FirstName: 'Alice' }
];

var filteredArray = sampleData.filter(function(item) {
  return item.LastName === 'Smith';
});

console.log(JSON.stringify(filteredArray));

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

Determine the originating component in React by identifying the caller

Can you explain how to access the calling component in a React application? function MyCallingComponent() { return <MyReadingComponent/> } function MyReadingComponent() { console.log(callingComponent.state) } ...

Registration of Laravel Vue.js components

I am currently working on a Vue.js application in conjunction with Laravel. To get started, I registered Vue.js like this: import Vue from 'vue'; import VueRouter from 'vue-router'; Vue.use(VueRouter); import App from './compone ...

Transitioning from left to right, picture smoothly scrolls into view using Way

I've explored various websites and even attempted to decipher a waypoint guide, but unfortunately, I haven't had any success. The scroll function doesn't seem to be working with the code below. (source: ) Any assistance on this matter would ...

AJAX success object encounters an uncaught type error

After successfully executing one of my AJAX Posts, there is a logical test with the returned "data" object. Surprisingly, upon page load, JavaScript throws an uncaught type error stating that it cannot read a property of undefined on this line: success: f ...

Node.js assert causes mocha to hang or timeout instead of throwing an error when using assert(false)

I am facing an issue with a mocha test that I have written: describe 'sabah', → beforeEach → @sabahStrategy = _.filter(@strats, { name: 'sabah2' })[0] .strat it 'article list should be populated&ap ...

Instructions for creating a scrollable unordered list when it reaches its maximum capacity

My HTML code has a <ul> element with the following structure: <ul id="messages"> </ul> In my HTML file, I have not specified any <li> items. However, in my JavaScript code using jQuery, I dynamically add <li> items to the & ...

How can PHP be utilized to work with multidimensional arrays and aggregate functions in MySQL?

UPDATED Is there a way to add an additional column next to 'u2' labeled CUMULATIVE TOTAL that displays the total number of students, total payable amount, total paid amount, and total balance based on counsellors? For example, if 'c1' ...

A guide on displaying a string returned from JavascriptExecutor in Java

Currently, I am attempting to retrieve the string output from JavascriptExecutor called within Java for the first time. While I have looked at various posts on SO, none seem to detail how to successfully extract the string into Java. After scouring the in ...

What is the best way to use the map() function in Julia to create a modified version of an array containing composite types?

Here's the code I've been working on: details = """'Swift', '2014', 'compiled'; 'Objective-C', '1984', 'compiled'; 'Scala', '2004', 'compiled&apos ...

What is the best way to incorporate a gratitude note into a Modal Form while ensuring it is responsive?

Currently, I have successfully created a pop-up form, but there are two issues that need fixing. The form is not responsive. After filling/submission, the form redirects to a separate landing page for another fill out. Expected Outcome: Ideally, I would ...

Sitepen DGrid does not make additional data queries when scrolling

Context: My app utilizes DGrid OnDemandGrid version 0.3.7 with a Memory Store containing all the data. However, with new requirements suggesting a backend store with over 400k rows, I created a custom dojo store based on the JSonRestStore. Challenge: Desp ...

Having trouble retrieving prices using an npm package

There is a more effective way to retrieve prices using the npm package, node-binance-api, rather than relying on the "coin" variable that I am currently struggling with. If anyone could assist me in finding a better solution or the optimal method for fetch ...

Alert received upon selecting the React icon button

In the login code below, I have utilized FaEye and FaEyeSlash react icons. However, every time I click on them, a warning message pops up. To avoid this issue, I attempted to switch from using tailwindcss to normal CSS. Login.jsx import { useContext, useS ...

I am attempting to incorporate a List View within a Scroll View, but they are simply not cooperating. My goal is to display a collection of items with additional text placed at the bottom

This is how it should appear: item item item item additional text here I am trying to create a layout where the list is in List View for benefits like virtual scrolling, but the entire layout needs to be within a Scroll View. I want to be able to con ...

Should the updater method be placed in the state or passed directly to the context?

Is it better to have this context setup like so: <MatchContext.Provider value={this.state.match}> Or should I structure it as follows in my state? match: { match: null, updateMatch: this.updateMatch }, Which approach is more eff ...

What are some methods to maintain active MySQL connections in a Node.js environment?

In my Node.js application, I have noticed that mysql connection pools expire after a period of idle time, resulting in delays when performing queries as new connections need to be created. This delay is unacceptable and I am seeking a solution. My idea is ...

The resolveMX function in Google Cloud Functions is encountering issues when trying to process a list of domains

Here is the task at hand. I have a large list of domains, over 100,000 in total, and I need to iterate through them using a foreach loop to resolve MX records for each domain. Once resolved, I then save the MX records into another database. Below is the c ...

Generating small image previews in JavaScript without distorting proportions

I am currently working on a client-side Drag and Drop file upload script as a bookmarklet. To prepare for the upload process, I am utilizing the File API to convert the images into base64 format and showcase them as thumbnails. These are examples of how m ...

Utilizing Node and Electron to dynamically adjust CSS style properties

Having a dilemma here: I need to access the CSS properties from styles.css within Electron. Trying to use document.getElementsByClassName() won't work because Node doesn't have document. The goal is to change the color of a specific div when the ...

Showing particular classes on an element using intersectionObserver/Scrollspy: a step-by-step guide

Here are three different sections on my Vue page. <section id="home">Home</section> <section id="about">About</section> <section id="contact">Contact</section> When I click on a Navbar Link ...