Returning an incorrect array occurs when there is only one piece of data in the JSON

I successfully transferred JSON data from the server-side to the client-side. However, I encountered an issue with a function that is supposed to return the latest rate and currency_id. This function works for some products but not for others.

When using the function for product_id 1 and 2, it functions properly and returns the correct array. However, when trying product_id 3 and 4, it only returns an empty array. The function in question is provided below:

JS function:

COMNAME.prepEdit = function (product_id) {
const currencyIds = [];
const result = [];

const sortData = COMNAME.newCurrency
    .filter((item) => item.product_id === product_id)
    .sort((item1, item2) => item1.id - item2.id);


for (let i = sortData.length - 1; i > 0; i--) {
    const item = sortData[i];

    if(!currencyIds.includes(item.currency_id)) {
        currencyIds.push(item.currency_id)
        result.unshift(item);
    }
}

return result;
}

I am seeking assistance in identifying the cause of this discrepancy. Your help is greatly appreciated. Thank you.

Answer №1

I'm a bit confused about your intentions here, but shouldn't your iteration loop be more along these lines:

for (let i = sortData.length - 1; i >= 0; i--) {
Maybe consider changing i>=0 if you also need to include the first index?

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 function d3.geoStitch has not been defined

I am currently working on implementing this example that visualizes a TIFF file using d3 as a node script. Everything seems to be functioning well, except when it comes to d3.geoStitch where my script throws an error stating d3.geoStitch is undefined. The ...

Struggling to figure out why jqueryrotate isn't functioning as expected

I'm having trouble getting example #2 of jqueryrotate from this page to work on a simple HTML page. Can someone help me figure out what I'm doing wrong? Thanks! Here is the code I'm using: <!DOCTYPE html> <head> <script typ ...

What is the module system that fabric composer utilizes for its logic JavaScript files?

I am currently in the process of creating a business-network-definition for Hyperledger using Fabric (based on generator-hyperledger-fabric). Everything has been running smoothly so far, but as we move onto our Proof of Concept (PoC), a few questions have ...

Implement a loader in AngularJS to display when transitioning between pages

Is there a way to implement a loader that appears when the page starts changing and only disappears once the entire page is fully rendered to prevent clipping bugs? I have already set up the loader as shown below: $scope.$on('$routeChangeStart' ...

Transform Java JSONArrays into Map Objects Effortlessly

I am currently working on a Java class that has the purpose of converting JSON into a Map by using a specific key. Below, you can find the desired method format and steps: public Map<String, Map<String, String>> convert(String jsonBody, String ...

Ways to customize nav-pills for restricting the page count shown

Attempting to establish a pagination system with the implementation of nav-pills. The unique aspect is that these nav-pills are dynamically generated, resulting in instances where there may be just one or potentially over 100. Utilizing bootstrap 4 & ...

The functionality of the ui sortable feature in Angular is not effective when used on a single-page website

My latest project involves using a single-page application. To connect the UI Angular library, I followed these steps: I started by adding the necessary scripts: <script src=.....jquery-1.9.1.js"></script> <script src=.....jquery-ui.js"> ...

Can you explain the functioning of knockout container less syntax? (does it have any drawbacks?)

There are numerous instances and examples of using knockout ContainerLess syntax, although I find it challenging to locate proper documentation from their site. Initially, my question was "is it evil?" but upon realizing my lack of understanding on how it ...

VS Code lacks autocomplete intellisense for Cypress

I am currently using Cypress version 12.17.1 on VS Code within the Windows 10 operating system. My goal is to write Cypress code in Visual Studio Code, but encountered an issue where the cypress commands that start with cy are not appearing as auto-comple ...

Show react-card-flip children conditionally based on a button, otherwise display one frontside and two backsides

Is it possible to achieve the following functionality using the react-flip-package? I have a card with two buttons on the front side. When I click one button, I want the card to flip to one backside, and when I click the other button, I want it to flip to ...

When an `angularjs select` is used with a filter, the first line may appear empty at first. However

I'm feeling a bit confused about why my ng-options is once again giving me an empty line with a filter applied. Could you please take a look at this plunker to see the issue? The goal is to show an org chart in a dropdown list that is based on a tre ...

Create queries for relays in a dynamic manner

I'm using Relay Modern for my client GraphQL interface and I am curious to know if it is possible to dynamically generate query statements within Relay Modern. For example, can I change the original query structure from: const ComponentQuery = graphq ...

Executing a callback function within two nested functions

I am having trouble with the callback function that is being called inside a function which in turn calls another function. exports.user = function(userName, pwd, callback) { db.User.findOne({'userName': userName}, function(error, obj) { ...

I am looking to modify the highlighted table cell whenever the value within it changes

I am currently working on a small project related to the Stock Market. In this project, I need to dynamically change the style of the td element based on data fluctuations - green highlight for an increase and red highlight for a decrease. In the provid ...

Translate the timestamp format of JSON date into a Java Object with LocalDatetime

Details of the Project: Utilizing Spring Boot with Gradle Here is my Java class: public class Sale{ private String storeNumber; @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") private LocalDateTime saleDateTime; // getter, ...

This function is designed to only work under specific conditions

Looking for assistance with a function that takes an item ID as input and changes its border when pressed. The goal is to increase the border width by 2px when there is no border, and remove the border completely when pressed again. Currently, only the f ...

The functionality of opening a link in a new tab is not functioning properly in Internet Explorer

When I use window.open in jQuery to open a link in a new tab, it works perfectly for me in Chrome, Safari, and Firefox. However, I am facing an issue in IE10 where it does not work. $('.div').click(function() { $(this).target = "_blank"; ...

Laravel tutorial: Implementing multiple output formats in a single controller method

I am looking for guidance on setting up the proper routes and models in order to retrieve a view with data when visiting www.myapp.com/search?keyword=test, and a JSON representation of the same search result when visiting api.myapp.com/search?keyword=test. ...

Destination format contains an invalid object in the property list. Unable to convert property list to JSON

Whenever I attempt to convert a Plist to JSON using the command plutil -convert json -, it throws an error because of an invalid object in the input. An example of this is: $ ioreg -rw0 -c AppleSmartBattery -a | plutil -convert json - <stdin>: invali ...

Showcasing the values of JavaScript

How can I resolve the issue depicted in the second picture? I need the value of 3 only in the USD column, with all others having a value of zero. <div class="span3"> <ul class="nav nav-tabs nav-stacked" > <?php foreach ( ...