Remove an item from a complex JSON structure based on the specified name. The function will then return the

Hey there, I'm just starting out with react native and I have an array of objects. My goal is to remove an inner object from this JSON data.

[
  {
    Key: 1,
    exchnageArr: [
      {
        name: ”FX”
      },
      {
        name: ”MK”
      }
    ]
  },
  {
    Key: 2,
    exchnageArr: [
      {
        name: ”CK”
      },
      {
        name: ”DK”
      }
    ]
  }
]

I need to get rid of the {name:"FX"} entry in this JSON data by passing "FX". Unfortunately, my attempted solution hasn't been successful. Can someone help me figure out how to do this?


    const newDatavalues = arr.forEach((item) =>
            item.exchangeArr.forEach((subItem, index) => {
              if (subItem.name === "FX") {
               return item.exchangeArr.splice(index, 1);
           } 
       })
     );

Answer №1

To filter out specific elements from arrays within objects, you can utilize the Array#filter method.

let arr=[{Key:1,exchnageArr:[{name:"FX"},{name:"MK"}]},{Key:2,exchnageArr:[{name:"CK"},{name:"DK"}]};
for (const o of arr)
  o.exchnageArr = o.exchnageArr.filter(x => x.name !== 'FX');
console.log(arr);

Answer №2

You made a small error with the property name exchnageArr, it should be exchangeArr. This mistake was found in your array (JSON).

Here is the corrected JSON:

[
  {
    Key: 1,
    exchangeArr: [
      {
        name: ”FX”
      },
      {
        name: ”MK”
      }
    ]
  },
  {
    Key: 2,
    exchangeArr: [
      {
        name: ”CK”
      },
      {
        name: ”DK”
      }
    ]
  }
]

Your code should work properly now.

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

Change the position of an HTML image when the window size is adjusted

My web page features a striking design with a white background and a tilted black line cutting across. The main attraction is an image of a red ball that I want to stay perfectly aligned with the line as the window is resized, just like in the provided gif ...

How can you direct a user to a specific page only when certain conditions are met?

Currently using React in my single page application. I have a good grasp on how Routes function and how to create a PrivateRoute. The issue arises when I need to verify the user's identity before granting access to a PrivateRoute. My attempt at imple ...

Get the @html.dropdownlistfor filtered for me

Currently, I am working on a project that requires me to filter the options in my second dropdown list based on the selection made in the first dropdown list. While the concept is clear, implementing it becomes tricky as I lack expertise in jQuery or JavaS ...

Canvas displaying inaccurately (unsteady)

While working on a simple HTML5/javascript game, I encountered an unusual issue with the canvas drawings - they seem to be unstable. I have a map that needs to be drawn in a specific way: The background consists of 2 layers: the ocean and the islands. Th ...

"Can you provide insight on retrieving asset files within a Dart package without any external dependencies

I recently developed a dart package for my flutter application and have encountered an issue with accessing a json file containing static data within the package. I am struggling to find a direct way to access asset files from a dart package code without r ...

Customizing the background color in TurnJSIncorporating

Recently, I encountered a problem that has left me puzzled. Despite searching online for help, I have not been able to find a solution. The issue lies with the basic code provided on the official page of the TurnJS script. <div id="flipbook"> <di ...

I am currently working on building a single-page application using React and Vite. However, I am facing an issue where the page is not rendering anything and just displaying a blank screen. Can anyone provide guidance on troubleshooting and

I am currently facing an issue while trying to build a react website using Vite. No matter what I do, nothing seems to render on the page. I even tried removing the react-router-dom and directly rendering the Home file, but still no luck. It appears that i ...

Looping through and replacing data in Joomla 2.5 using PHP foreach

Hello everyone! I am in the process of developing a component for Joomla 2.5 that utilizes jvectormap. However, I seem to be encountering an issue where only the most recent firms are being displayed in the JSON output, and not the older ones. I suspect th ...

I'm having trouble with my Laravel edit page not functioning properly when using vue.js. Can anyone help me troubleshoot

Currently, I am developing a dashboard to display details. Users can click on the edit button to modify their information. However, when I try to edit by clicking the button, nothing happens. It seems like the editing feature is not functioning properly, a ...

retrieving the values listed on the current v-data-table page

In my vuejs2 project, I am utilizing a v-data-table to display information in columns about a large number of users. Each page shows 25 users, with a total exceeding 60,000 individuals. I am wondering if there is a way to retrieve the list of users curre ...

Ways to convert asynchronous operations of Node.js into synchronous operations in Node.js

Using node js, I am making multiple AWS API calls within a for loop. var prodAdvOptions = { host : "webservices.amazon.in", region : "IN", version : "2013-08-01", path : "/onca/xml" }; prodAdv = aws.createProdAdvCli ...

What changes occurred to module file names following the process of minification?

I'm currently troubleshooting an issue with this particular code snippet: import globalComponents from './global-components'; // ... globalComponents.forEach((component) => { // eslint-disable-next-line no-underscore-da ...

Choose a JavaScript function by clicking on the HTML text

As a beginner in the world of coding, I have been diving into JavaScript, HTML, and CSS. Inspired by fictional supercomputers like the Batcomputer and Jarvis, I've challenged myself to create my own personal assistant to manage tasks, games, programs, ...

The subsequent block within the code is being initiated following the initial block in Node.js

It was expected that "1" would be printed before "2", but the output is incorrect. fs.readdir("./my_stocks", (err, files) => { for(each in files){ var file=files[each]; if(file!='portfolio.js'){ var fn="./my_sto ...

Leveraging the node CLI tool as a library for trimming MP3 files (trimp3

I recently came across a fantastic library that I am interested in using for my nodejs project: https://github.com/kyr0/trimp3 The only issue is that it functions as a cli tool, and I would like to integrate it seamlessly into my codebase as a library. D ...

Questions regarding prototype-based programming in Javascript

I am interested in achieving the following using Javascript: function A(){ this.B = function() { ... }; this.C = function() { <<I need to call B() here>> } ; }; I came across a method of method overloading, but I am curious to know ...

Is there a way to add two properties with the same name in order to enable the deserialization of two distinct JSON formats?

Looking for a way to parse a JSON element that can be either a single object or an array of objects without creating separate classes for each scenario. public name name { get; set; } public name[] name { get; set; } Several suggestions on Stack Overfl ...

How can I remove a quadratic curved arrow tool in Fabric JS canvas after it has been created?

I'm currently working on developing a tool for creating quadratic curved arrows. For reference, I utilized a demo from the following link to build the tool: I have successfully created the tool as per my requirements. However, I am encountering some ...

Retrieve information from a database by utilizing AJAX and store it in a JavaScript array

I'm facing an issue where I can retrieve data from the PHP file, but not from the database to my JavaScript code. I am using Ajax to fetch the data from the database, then passing it to the PHP file, and finally trying to filter this data using JavaSc ...

Utilize Photoshop's Javascript feature to extract every layer within the currently active document

Looking for insights on a Photoshop scripting issue. I have written a solution but it's not producing the correct result. Can anyone provide feedback on what might be wrong with the code? The goal is to retrieve all the layers in a document. Here is ...