Generate a fresh array using the information extracted from a JSON file

I need assistance in extracting a new array from JSON data. The desired output should be an array containing "itog" values.

[12860498,20156554,19187309]

[
      {
        "0": {
          "itog": 12860498,
          "return": 1107294,
          "beznal": 10598131
        },
        "date": "2021-01-31"
      },
      {
        "0": {
          "itog": 20156554,
          "return": 1147363,
          "beznal": 18127393
        },
        "date": "2021-02-28"
      },
      {
        "0": {
          "itog": 19187309,
          "return": 1667656,
          "beznal": 17597434
        },
        "date": "2021-03-31"
      }
    ]

Answer №1

const numbers = [
  {
    "0": {
      "total": 12860498,
      "refund": 1107294,
      "noncash": 10598131
    },
    "date": "2021-01-31"
  },
  {
    "0": {
      "total": 20156554,
      "refund": 1147363,
      "noncash": 18127393
    },
    "date": "2021-02-28"
  },
  {
    "0": {
      "total": 19187309,
      "refund": 1667656,
      "noncash": 17597434
    },
    "date": "2021-03-31"
  }
];

const finalAmounts = numbers.map(function(item) {
  return item[0].total;
});

console.log( finalAmounts );

Answer №2

If you're looking to retrieve specific data from an array, you can leverage the power of Array.map.

var input = new Array({
    "0": {
      "itog": 12860498,
      "return": 1107294,
      "beznal": 10598131
    },
    "date": "2021-01-31"
  },
  {
    "0": {
      "itog": 20156554,
      "return": 1147363,
      "beznal": 18127393
    },
    "date": "2021-02-28"
  },
  {
    "0": {
      "itog": 19187309,
      "return": 1667656,
      "beznal": 17597434
    },
    "date": "2021-03-31"
  }
);

var output = input.map(item => item[0]['itog']);
console.log(output);

Answer №3

Kindly consider utilizing one of the proposed solutions as they align perfectly with your requirements.

For a more general approach, you may consider:

const data = [
  {
    "0": {
      "itog": 12860498,
      "return": 1107294,
      "beznal": 10598131
    },
    "date": "2021-01-31"
  },
  {
    "0": {
      "itog": 20156554,
      "return": 1147363,
      "beznal": 18127393
    },
    "date": "2021-02-28"
  },
  {
    "0": {
      "itog": 19187309,
      "return": 1667656,
      "beznal": 17597434
    },
    "date": "2021-03-31"
  }
];

let itogs = [];
data.forEach(entry => {
  itogs = [
    ...itogs,
    ...Object.values(entry)
      .map(obj => obj['itog'])
      .filter(itog => itog)
  ];
});


console.log(itogs);

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

The perpetual loop in React context triggered by a setState function within a useEffect block

I'm experiencing an endless loop issue with this context component once I uncomment a specific line. Even after completely isolating the component, the problem persists. This peculiar behavior only manifests when the row is discounted and the browser ...

Error: PHP is showing an undefined index error when trying to decode JSON, even though the value

I am feeling quite puzzled. I transferred a key value pair object from jQuery to PHP and successfully alerted it back out. However, when I visit the PHP page, it indicates that the data is either null or an undefined index. Here is my jQuery code: $(&ap ...

What is the process of converting exactPageList from any to any[] before assigning it to pagefield?

Encountering an issue with paging on my angular 7 app where I am unable to assign exactpagelist of any type to pagefield of type array. The problem seems to be occurring on the last line of the function totalNoOfPages at this point: this.pageField = this ...

Tips and techniques for implementing push notifications in front-end applications without the need for a page refresh

I am working on a Python program that inserts data into a XAMPP database. I am looking for a way to continuously monitor the database for any changes and automatically send updates to the frontend without relying on click events. Is there a method simila ...

Focusing on a text field after reloading a different div with AJAX

I've been spending a lot of time figuring out the following issue and I'm hoping someone can help me find the solution. My web application has an input field (type="text") that is ready to accept user input when the page loads. When the user nav ...

Implementing recursive functionality in a React component responsible for rendering a dynamic form

Hello to all members of the Stack Overflow community! Presently, I am in the process of creating a dynamic form that adapts based on the object provided, and it seems to handle various scenarios effectively. However, when dealing with a nested objec ...

What is the method to alter the color of an SVG source within an image tag?

I am currently working on a component that involves changing the color of an SVG icon from black to white when a color prop is given. export class CategoryIconComponent extends React.Component { static displayName = 'CategoryIcon'; state = ...

What is the best way to transform JSON or an array from an AJAX responseText into a JavaScript array?

I recently implemented ajax into my code, and it is working perfectly. It provides me with either JSON or an array as the output. Here is the snippet of code I used: xmlhttp=new XMLHttpRequest(); xmlhttp.open("GET","http://map_ajax_control.php",false); xm ...

Even after implementing a setTimeout function, both Promises and Axios requests are still resulting in a

Recently, I've encountered an issue with the Reverse Geocode API from TomTom. The problem arises when I send multiple latitude and longitude coordinates (around 72 of them) to the API, resulting in frequent 429 responses. To address this issue, I att ...

External JavaScript files are also subject to the same origin policy

Suppose a website http://www.mysite.com contains an external JavaScript file added like this: <script src="http://www.yoursite.com/new.js"></script> Within the http://www.yoursite.com/new.js JavaScript file, there is an AJAX call to a script ...

Transition one background image into another using background positioning

Snippet of code: https://jsfiddle.net/foy4m43j/ HTML: <div id="background"></div> CSS: #background { background-image: url("http://i.imgur.com/hH9IWA0.png"); background-position: 0 0; background-repeat: repea ...

Arranging elements on top of a fixed element using JavaScript and CSS

Currently, I am implementing Javascript code that adds a div to the body of pages. The purpose of this div is to always stay at the top of the document or window, regardless of the page's design and content. Everything was functioning correctly until ...

How to incorporate "selectAllow" when dealing with dates in FullCalendar

I've been attempting to restrict user selection between two specific dates, but so far I haven't been able to make it work. The only way I have managed to do it is by following the routine specified in the businessHours documentation. Is there a ...

Calculate the combined sum of values within dynamic inputs that share a common class, and automatically update the sum whenever input values are altered or new rows are added or removed dynamically

$("#add-btn").click(function() { $("#dynamic").append('<tr>' + '<td class="td">' + '<input type="number" name="Debit" class="form-control Debit"/>' + '</td>' + '<td c ...

What are some methods to make sure that functions in AngularJS do not run at the same time

I am new to the world of JavaScript and I have encountered a problem with one of my functions. The function is designed to retrieve cached resources, but if the resource is not found in the cache, it makes a call to the back-end. The issue arises when thi ...

What is the reason why createServer() is often not recognized as a function?

After installing express globally and npm on my express app, I am encountering issues with both intellisence and the app itself (I am using visual studio code on mac OS Yosemite). Below is a snippet of the code: /// <reference path="typings/node/node. ...

React: the props appear to be undefined when they should actually have a value

I have a requirement for my props in the children class to be an array of Event objects. Prior to using it in App.js, I am checking if the array is empty like this: function App() { class Event { constructor(id, title, date){ this.id = id; ...

Discover how to utilize images encoded in base64 format within webhook embeds on Discord

Can someone help me with inserting an image into a Discord embed using a webhook? I have the image saved as a base64 string obtained from a database. However, my attempts so far have only resulted in an empty embed. const data = b64image.split(',&ap ...

Is it possible to determine HTML5 Context Menu Support using JavaScript?

While reviewing the Modernizr documentation, I couldn't find any information on creating a menu element and then checking its existence. However, I am concerned that even if the browser supports this, it may not support the type context. Do you have a ...

Nuxt.js Button Dropdown Widget

Currently, I am in the process of creating a button similar to a dropdown. This particular button is meant for printing and downloading documents. Here is the code I have implemented: <template> <v-menu transition="s ...