Skip to Content

Array in Json, entries are duplicated

I have a table in my view. With the button selection just after the date, I run a calendar and I have an array at result (29, 30 and 31th of January). The calendar provides dates. And I want to duplicate the line and change the # and the date.

When I try to insert the new lines in my model, it's impossible to have different dates and to change the ID

I've tried several option but all the time my information are duplicated during the push

At the end of the 3 push, all dates stored are the 29th of January even I've changed the value of vitem during the for.

           // Model definition
            var oAppModel = this.getOwnerComponent().getModel("application");
            var oAppData = oAppModel.getData();

            // Change of title of calendar
            var oCalendar = sap.ui.core.Fragment.byId("SelectMultiItemDialog", "multiDatesCalendar");
            var oTitle = this._oMultiDatesSelectionDialog.getTitle();
            var oItem = oTitle.substring(1, oTitle.indexOf(")"));

            // Get index of concerned item
            var sIndex = "";
            for (var i = 0; i < oAppData.shoppingCartItems.length; i++) {
                if (oAppData.shoppingCartItems[i].Item.toString() === oItem) {
                    sIndex = i;
            } // for i

            // Change dates on new lines
            var sLenIni = oAppData.shoppingCartItems.length - 1;
            var oTable = this.byId("shoppingCartItemsTable"),
                aItems = oTable.getItems(),
                l = 10;

            // Add new items 
            var aSelectedDates = oCalendar.getSelectedDates();
            var vItem = oAppData.shoppingCartItems[sIndex];
            for (var j = aSelectedDates.length - 1; j >= 0; j--) {
                vItem.requestedDeliveryDate = aSelectedDates[j].getStartDate();
                if (j !== 0) {
                    oAppData.shoppingCartItems.push(vItem); // <======== Problem

            } // for j 

Here are some copy of debug

err1.jpg (58.2 kB)
err2.jpg (88.8 kB)
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    Jan 08 at 01:11 PM
    oAppData.shoppingCartItems.push(JSON.parse(JSON.stringify(vItem))); // <======== Problem

    check if it helps

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 09 at 07:38 AM

    Good, it works now

    What is the meaning of this command ? (I'm a little newbie)

    Add comment
    10|10000 characters needed characters exceeded

    • make copy.

      previously you display the same variable three times....

      please accept the answer if your problem is solved