What is the best way to sift through an array containing arrays?

Looking for help with filtering and pushing data from a JSON array? Specifically, I need to filter arrays where the data's attrs property includes a src attribute. It's proving challenging for me, so any assistance would be greatly appreciated.

This is what my JSON data looks like:

DATA:

[
 {
  "data":{},
   "type":"image",
    "attrs":{
     "x":92,
     "y":163,
     "width":100,
     "height":100,
     "src":"http://localhost:63342/wodrobs/app/scripts/views/img/top.jpg",
   "cursor":"move",
   "opacity":1
   },
     "transform":"",
   "id":0
},
{
   "data":{},
   "type":"path",
   "attrs":{
   "fill":"none",
   "stroke":"#000",

     "stroke-dasharray":"- ",
    "opacity":0.5
  },
   "transform":"",
   "id":17
}
]

Answer №1

After analyzing your pseudo-JSON data, it seems like you can achieve the desired outcome by following this code snippet:

// Here is your data
var data = [
          {'src':"a.src"}, 
           {'id':"someid"},
          {'src':"b.src"} 
];

// Initialize an empty array to store results
var resultArray = [];

// Loop through the data array and filter based on 'src' property presence
for(i=0; i<data.length;i++){
  var element = data[i];
  if(element.src){
    resultArray.push(element);
  }
}

// Output the filtered result
console.log(resultArray);

Check out this link for a live demo.

Answer №3

Found the solution. I just realized my mistake.

 const filteredData = _.filter(jsonData, (data) => {
            return data.attrs.src;
        });

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

Remove JSON information with PHP When a button in an HTML table is clicked

I attempted to remove a JSON entry from the JSON file by clicking a button within an HTML tableview image here Here is my JSON data:[{"room_id":"1","room_type":"Duplex","room_location":"North",&q ...

Develop a custom dropdown menu using JavaScript

I've been working on creating a dropdown menu that appears after selecting an option from another dropdown menu. Here's the HTML code I'm using: <br> <select id ="select-container" onchange="addSelect('select-container') ...

Unlock the Power of EmailJS with Vue.js 2 and TypeScript

I couldn't find a similar issue online, so here's my problem. I'm trying to create a form for receiving contact from an app using Vue.js 2 and TypeScript. Here is my code: <form ref="form" class="form-data" @submit.pr ...

The custom tab component in React is currently not accepting the "disabledTabs" prop

I have designed a tab component as shown below: tab/index.jsx import React from 'react'; import TabHeader from './header'; import TabBody from './body'; import TabHeaderList from './header/list'; import TabBodyList ...

Converting Django arrays into strings

I have an array-like data stored in a database, Table A instant language 1 english 1 Indonesia 2 japan 2 korea 2 british This is the models.py file class A(models.Model): instant = models.ForeignKey(Ins ...

Highcharts automatically hide series that are not active from the legend when capturing a screenshot

Is there a way to only display active series in the legend when capturing a screenshot? Imagine having a chart like the following: https://i.sstatic.net/9enrC.jpg But I would like the screenshot to show only the active series, like this: https://i.sstat ...

Is TypeScript checking in VSCode failing to detect await functions?

I have been working on an app that retrieves weather data based on a user's location, and everything seems to be functioning correctly. However, during the coding process, I keep encountering errors that are being flagged, even though the code runs sm ...

How can I position two divs side by side within an Appbar?

I would like the entire Container to be in a single row, with the Typography centered as it already is, and the toggle-container to float to the right <AppBar className={styles.AppBar}> <Toolbar> <Container> ...

Ways to utilize jQuery for intricate forms in place of RJS

Within the 74th episode of Railscasts, Ryan demonstrates the process of executing intricate forms with RJS. Check it out here: The key here is to generate a partial and incorporate it using RJS. This episode is quite dated and back then, jQuery may not ha ...

React component failing to update when props change

After loading correctly, my react component fails to re-render when props change. Despite the fact that render() is being called and the list array contains the correct data according to console.log(list), the page does not refresh. Adding a setState in co ...

Using $http call to pass attributes to controller in Web Api 2 using attribute routing

I am currently working on an $http call in my project: $http({ method: 'GET', url: '/api/PhotoSubmit/GetCategories', accept: 'application/json' }) .success(function (result) { ...

What causes Firefox's CPU to spike to 100% when a slideshow begins that adjusts the width and left coordinates of certain divs?

Seeking Advice I'm in need of some help with identifying whether the code I'm working on is causing high CPU usage in Firefox or if it's a bug inherent to the browser itself. The situation is getting frustrating, and I've run out of so ...

Tips on utilizing a .env file with a JSON that includes a backslash symbol?

Background: I am working with a MSSQL server database that uses specific instances. Therefore, the connection string/engine to this database would appear as follows: Engine(mssql+pyodbc://User:Password@servername\instance,5555/database?driver=ODBC+Dr ...

Modifying css background in real-time based on the current weather conditions

Is there a way to dynamically change the background image in CSS based on the weather condition? I'm utilizing the wunderground API to retrieve the weather data, which is stored in the weather variable. I am struggling with how to update the backgrou ...

How can I implement a single-column search feature in a GridView using Javascript in ASP.NET?

I found a Google function for client-side searching in a grid using a textbox Here is the function: <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script> function searchFunction(phrase, ...

Mobile phone carousel glitch

I am encountering an issue with the Bootstrap v4.0 Carousel that I can't seem to resolve. Despite searching online for similar problems, I have not been able to find a solution, which is quite perplexing. Whenever I load a page on an iPhone, the caro ...

What is the best way to iterate through a collection of two or more arrays in order to determine the total length of all

https://i.stack.imgur.com/PpFlB.pngI currently have multiple Arrays containing various inputs this.listNumber = [ { "GenericQuestions": [ { "input": "long", }, { "input": & ...

How can I use D3.js to form a circular group in an organization structure, link it with a circular image, and connect

Is it possible to create a radial grouped circle using d3.js, similar to the image below: https://i.sstatic.net/1Hwd2.jpg I have written some code as shown below. However, I am facing challenges in connecting every circle with a curved line and displayi ...

Shared Vue configuration settings carrying over to Jest spec files

For my unit testing of components using VueJS and Jest, I'm incorporating the Bootstrap Vue library for styling. To address console warnings regarding unknown plugins, I've set up a configuration file: import { createLocalVue } from '@vue/t ...

HTML table containing radio buttons styled with Font Awesome icons

I'm having some trouble getting a radio button with Font Awesome to work properly within an HTML table. Outside of the table, it functions as expected, but inside the table, it only seems to hide/show between the two states without displaying the chec ...