更新单个元素后,JSON数组上的错误

时间:2016-05-16 06:02:40

标签: arrays json

这是方案,

HTML,

<div id="output">

</div>

脚本,

var requestedItemsCache=[  
   {  
      "DetailID":40085,
      "PRNo":33420,
      "Total":31400,
      "Justification":null,
      "SAPAssetCode":null,
      "InventoryDetails":null,
      "DeliveryDate":null,
      "DeploymentDate":null,
      "CurrentApproverID":null,
      "CurrentApproval":false,
      "Specification":null,
      "StockID":null,
      "QuotedPrice":null,
      "QuotedTotalPrice":null,
      "PONo":null,
      "Remarks":"For PO",
      "DeptStockID":null,
      "Confirmation":null,
      "Flag":0,
      "ApprovalFlag":0,
      "SerialNo":null,
      "SubItems":[  
         {  
            "DetailID":40086,
            "PRNo":33420,
            "Total":0,
            "Justification":null,
            "SAPAssetCode":null,
            "InventoryDetails":null,
            "DeliveryDate":null,
            "DeploymentDate":null,
            "CurrentApproverID":null,
            "CurrentApproval":false,
            "Specification":null,
            "StockID":null,
            "QuotedPrice":null,
            "QuotedTotalPrice":null,
            "PONo":null,
            "Remarks":"For PO",
            "DeptStockID":null,
            "Confirmation":null,
            "Flag":0,
            "ApprovalFlag":0,
            "SerialNo":null,
            "SubItems":null,
            "ParentID":null,
            "ItemID":1051,
            "ItemDescription":"CPU Core i3, HDD 500GB SATA,  RAM 4GB, Windows 7 Professional, 64-bit",
            "Price":0,
            "ItemCatSubID":1063,
            "SubCategoryName":"Peripherals & Accessories",
            "Brand":"Lenovo",
            "Model":"Thinkcentre M72e",
            "Unit":null,
            "UnitID":0,
            "ItemHeaderID":1023,
            "GenericName":"CPU",
            "ItemCatID":0,
            "Quantity":1,
            "IsActive":0,
            "IsComponent":0,
            "isAsset":1,
            "SubCatalogs":null
         },
         {  
            "DetailID":40087,
            "PRNo":33420,
            "Total":0,
            "Justification":null,
            "SAPAssetCode":null,
            "InventoryDetails":null,
            "DeliveryDate":null,
            "DeploymentDate":null,
            "CurrentApproverID":null,
            "CurrentApproval":false,
            "Specification":null,
            "StockID":null,
            "QuotedPrice":null,
            "QuotedTotalPrice":null,
            "PONo":null,
            "Remarks":"For PO",
            "DeptStockID":null,
            "Confirmation":null,
            "Flag":0,
            "ApprovalFlag":0,
            "SerialNo":null,
            "SubItems":null,
            "ParentID":null,
            "ItemID":1052,
            "ItemDescription":"LCD Monitor LCD Display 19” ",
            "Price":0,
            "ItemCatSubID":1063,
            "SubCategoryName":"Peripherals & Accessories",
            "Brand":"Lenovo",
            "Model":"ThinkVision L197Wa",
            "Unit":null,
            "UnitID":0,
            "ItemHeaderID":1019,
            "GenericName":"Monitor",
            "ItemCatID":0,
            "Quantity":1,
            "IsActive":0,
            "IsComponent":0,
            "isAsset":1,
            "SubCatalogs":null
         },
         {  
            "DetailID":40088,
            "PRNo":33420,
            "Total":0,
            "Justification":null,
            "SAPAssetCode":null,
            "InventoryDetails":null,
            "DeliveryDate":null,
            "DeploymentDate":null,
            "CurrentApproverID":null,
            "CurrentApproval":false,
            "Specification":null,
            "StockID":null,
            "QuotedPrice":null,
            "QuotedTotalPrice":null,
            "PONo":null,
            "Remarks":"For PO",
            "DeptStockID":null,
            "Confirmation":null,
            "Flag":0,
            "ApprovalFlag":0,
            "SerialNo":null,
            "SubItems":null,
            "ParentID":null,
            "ItemID":1053,
            "ItemDescription":"Keyboard Qwerty, USB",
            "Price":0,
            "ItemCatSubID":1063,
            "SubCategoryName":"Peripherals & Accessories",
            "Brand":"Lenovo",
            "Model":"SK-8820",
            "Unit":null,
            "UnitID":0,
            "ItemHeaderID":1018,
            "GenericName":"Keyboard",
            "ItemCatID":0,
            "Quantity":1,
            "IsActive":0,
            "IsComponent":0,
            "isAsset":1,
            "SubCatalogs":null
         },
         {  
            "DetailID":40089,
            "PRNo":33420,
            "Total":0,
            "Justification":null,
            "SAPAssetCode":null,
            "InventoryDetails":null,
            "DeliveryDate":null,
            "DeploymentDate":null,
            "CurrentApproverID":null,
            "CurrentApproval":false,
            "Specification":null,
            "StockID":null,
            "QuotedPrice":null,
            "QuotedTotalPrice":null,
            "PONo":null,
            "Remarks":"For PO",
            "DeptStockID":null,
            "Confirmation":null,
            "Flag":0,
            "ApprovalFlag":0,
            "SerialNo":null,
            "SubItems":null,
            "ParentID":null,
            "ItemID":1054,
            "ItemDescription":"Mouse Optical, USB ",
            "Price":0,
            "ItemCatSubID":1063,
            "SubCategoryName":"Peripherals & Accessories",
            "Brand":"Lenovo",
            "Model":"LXB MO28UOA ",
            "Unit":null,
            "UnitID":0,
            "ItemHeaderID":1016,
            "GenericName":"Mouse",
            "ItemCatID":0,
            "Quantity":1,
            "IsActive":0,
            "IsComponent":0,
            "isAsset":1,
            "SubCatalogs":null
         }
      ],
      "ParentID":null,
      "ItemID":1016,
      "ItemDescription":"Lenovo Desktop; Office Worker 1 as General User Core i3, HDD 500GB SATA,  RAM 4GB, LCD Display 20”, Windows 7 Professional, 64-bit, CPU Warranty 5/5/5, Monitor Warranty 3/3/3",
      "Price":31400,
      "ItemCatSubID":1061,
      "SubCategoryName":"Computers",
      "Brand":"Lenovo",
      "Model":"Thinkcentre M72e",
      "Unit":null,
      "UnitID":0,
      "ItemHeaderID":1001,
      "GenericName":"Desktop for Office Worker 1 (General User)",
      "ItemCatID":0,
      "Quantity":1,
      "IsActive":0,
      "IsComponent":1,
      "isAsset":1,
      "SubCatalogs":null
   }
];

var additionalData={"ItemID":1016,"ItemDescription":"Core i3, HDD 500GB SATA,  RAM 4GB, LCD Display 20”, Windows 7 Professional, 64-bit, CPU Warranty 5/5/5, Monitor Warranty 3/3/3","Price":31400,"ItemCatSubID":1061,"SubCategoryName":"Computers","Brand":"Lenovo","Model":"Thinkcentre M72e","Unit":"Unit","UnitID":478,"ItemHeaderID":1001,"GenericName":"Desktop for Office Worker 1 (General User)","ItemCatID":1001,"Quantity":1,"IsActive":1,"IsComponent":1,"isAsset":1,"SubCatalogs":[{"ItemID":1051,"ItemDescription":"Core i3, HDD 500GB SATA,  RAM 4GB, Windows 7 Professional, 64-bit","Price":27500,"ItemCatSubID":1063,"SubCategoryName":"Peripherals & Accessories","Brand":"Lenovo","Model":"Thinkcentre M72e","Unit":"Unit","UnitID":478,"ItemHeaderID":1023,"GenericName":"CPU","ItemCatID":1001,"Quantity":1,"IsActive":1,"IsComponent":0,"isAsset":1,"SubCatalogs":null},{"ItemID":1052,"ItemDescription":"LCD Display 19” ","Price":0,"ItemCatSubID":1063,"SubCategoryName":"Peripherals & Accessories","Brand":"Lenovo","Model":"ThinkVision L197Wa","Unit":"Unit","UnitID":478,"ItemHeaderID":1019,"GenericName":"Monitor","ItemCatID":1001,"Quantity":1,"IsActive":1,"IsComponent":0,"isAsset":1,"SubCatalogs":null},{"ItemID":1053,"ItemDescription":"Qwerty, USB","Price":0,"ItemCatSubID":1063,"SubCategoryName":"Peripherals & Accessories","Brand":"Lenovo","Model":"SK-8820","Unit":"Unit","UnitID":478,"ItemHeaderID":1018,"GenericName":"Keyboard","ItemCatID":1001,"Quantity":1,"IsActive":1,"IsComponent":0,"isAsset":1,"SubCatalogs":null},{"ItemID":1054,"ItemDescription":"Optical, USB ","Price":600,"ItemCatSubID":1063,"SubCategoryName":"Peripherals & Accessories","Brand":"Lenovo","Model":"LXB MO28UOA ","Unit":"Unit","UnitID":478,"ItemHeaderID":1016,"GenericName":"Mouse","ItemCatID":1001,"Quantity":1,"IsActive":1,"IsComponent":0,"isAsset":1,"SubCatalogs":null}],"Flag":1000,"ApprovalFlag":1,"Total":31400,"SubItems":null};


requestedItemsCache.push(additionalData);
requestedItemsCache.push(additionalData);
requestedItemsCache.push(additionalData);

var x=document.getElementById("output");

/*RENDER ORIGINAL SCHEMA*/
x.innerHTML = "Original Value : " + x.innerHTML + requestedItemsCache[0].Flag + "<br/>";

requestedItemsCache[0].Flag=2000;
x.innerHTML = x.innerHTML+ "Update Value : "  + requestedItemsCache[0].Flag + "<br/><br/> THIS IS OK! </br/><br/>";



/*ORIGINAL VALUE*/
x.innerHTML =x.innerHTML+"Original Value Index 1 : " +(requestedItemsCache[1].Flag) + "<br/>";
x.innerHTML =x.innerHTML+"Original Value Index 2 : " +(requestedItemsCache[2].Flag) + "<br/>";
x.innerHTML =x.innerHTML+"Original Value Index 3: " +(requestedItemsCache[3].Flag) + "<br/><br/><br/";

/*BUG IN UPDATING JSON OBJECT*/
x.innerHTML = x.innerHTML+ " Update element at index 1" + "<br/><br/><br/>";

requestedItemsCache[3].Flag=3000;

x.innerHTML =x.innerHTML+"Updated Value Index 1 : " +(requestedItemsCache[1].Flag) + "<br/>";
x.innerHTML =x.innerHTML+"Updated Value Index 2 : " +(requestedItemsCache[2].Flag) + "<br/>";
x.innerHTML =x.innerHTML+"Updated Value Index 3: " +(requestedItemsCache[3].Flag) + "<br/>";

console.log(requestedItemsCache[1].Flag);
console.log(requestedItemsCache[2].Flag);
console.log(requestedItemsCache[3].Flag);

JSFiddle

1 个答案:

答案 0 :(得分:0)

你的问题来自那里:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace windowsForApplication15
{
 public partial class Form1 : Form
{
    public enum Directions
    {
        right,
        left,
        up,
        down
    }

    private Directions car_direction;

    //value for fall speed
    int G = 15;
    int force;

    //determines wheather car is gonna jump or not
    bool jump;



    //boxes from right
    public PictureBox[] right_matrix = new PictureBox[5];
    public Form1()
    {
        InitializeComponent();


        jump = false;
    }

    /// <summary>
    /// timer for controlling car
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Form1_Load(object sender, EventArgs e)
    {
        //to creating 5 boxes
        create_box(5);

    }

    private void timer1_Tick(object sender, EventArgs e)
    {

        //if jump is true
        if (jump)
        {
            //start falling
            car.Top -= force;
            force -= 1;
        }
        //being able to stay top of block(I couldun't make my car stay specific point when falling is stop so created a block ,which is transparent in my winform 
        //so car can stop when faling is finish)
        if (car.Left + car.Width - 1 > block.Left && car.Left + car.Width + 5 < block.Left + block.Width + car.Width
            && car.Top + car.Height >= block.Top && car.Top < block.Top)
        {
            car.Top = panel1.Height - block.Height - car.Height;
            force = 0;
            jump = false;
        }


    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Right && car_direction != Directions.right)
        {
            car.Location = new Point(car.Location.X + 130, car.Location.Y);
            car_direction = Directions.right;
        }

        if (e.KeyCode == Keys.Left && car_direction != Directions.left)
        {
            car.Location = new Point(car.Location.X - 130, car.Location.Y);
            car_direction = Directions.left;
        }

        if (!jump && e.KeyCode == Keys.Up)
        {
            jump = true;
            force = G;
        }

    }

    /// <summary>
    /// craeting boxes,which are gonne be what want to avoid from
    /// </summary>
    /// <param name="value"></param>
    private void create_box(int value)
    {
        Random rnd = new Random();
        for (int i = 0; i <= value; i++)

        {
            int number = rnd.Next(0, 7);
            PictureBox boxes_right = new PictureBox();
            panel1.Controls.Add(boxes_right);
            boxes_right.Width = 50;
            boxes_right.Height = 15;
            boxes_right.BorderStyle = BorderStyle.FixedSingle;
            boxes_right.Top = (i * 20) + 152;
            boxes_right.Left = 312;
            right_matrix[i] = boxes_right;
            right_matrix[i].Visible = true;

            switch (number)
            {
                case 1:
                    boxes_right.BackColor = Color.Red;
                    break;
                case 2:
                    boxes_right.BackColor = Color.BlueViolet;
                    break;
                case 3:
                    boxes_right.BackColor = Color.Pink;
                    break;
                case 4:
                    boxes_right.BackColor = Color.Black;
                    break;
                case 5:
                    boxes_right.BackColor = Color.Brown;
                    break;
                case 6:
                    boxes_right.BackColor = Color.MistyRose;
                    break;
                case 7:
                    boxes_right.BackColor = Color.Green;
                    break;
            }

        }
    }



    /// <summary>
    /// it is used to controlling boxes movement
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void timer2_Tick(object sender, EventArgs e)
    {

        Random rastgele = new Random();

        //shifting boxes aganist bottom of form.
        for (int i = 0; i < 1; i++)
        {
            int number_2 = rastgele.Next(0, 7);
            right_matrix[i].Top += (right_matrix[i].Top / 28) - 2;

            right_matrix[i].Width += +1;

            //if collision is true 
            if (Collision(right_matrix[i], car))
            {
                //stop application
                Application.Exit();
            }

            //if box  touches the block
            if (right_matrix[i].Bottom > block.Top + 25)
            {
                //make it be in from the top
                right_matrix[i].Top = 152;
                right_matrix[i].Width = 50;
                right_matrix[i].Height = 15;

                //for changing boxes color
                switch (number_2)
                {
                    case 1:
                        right_matrix[i].BackColor = Color.Red;
                        break;
                    case 2:
                        right_matrix[i].BackColor = Color.BlueViolet;
                        break;
                    case 3:
                        right_matrix[i].BackColor = Color.Pink;
                        break;
                    case 4:
                        right_matrix[i].BackColor = Color.Black;
                        break;
                    case 5:
                        right_matrix[i].BackColor = Color.Brown;
                        break;
                    case 6:
                        right_matrix[i].BackColor = Color.MistyRose;
                        break;
                    case 7:
                        right_matrix[i].BackColor = Color.Green;
                        break;
                }
            }


        }
    }

    /// <summary>
    /// To finish game when car crushes box
    /// </summary>
    /// <param name="Object1"></param>
    /// <param name="Object2"></param>
    /// <returns></returns>
    private bool Collision(dynamic Object1, dynamic Object2)
    {

        bool Collided = false;
        if (Object1.Top + Object1.Height <= Object2.Top + 5 & Object1.Top + Object1.Height >= Object2.Top & Object1.Left == Object2.Left)
        {
            if (Object1.Visible == true & Object2.Visible == true & jump == false)


            {
                Collided = true;
            }
        }

        return Collided;
    }


}
}

您将相同的对象推送到requestedItemsCache,因此当您稍后在一个引用中进行更改时,其他引用也会收到修改,因为它们是同一个对象。

如果您使用以下内容,则不会发生:

var additionalData={...};

requestedItemsCache.push(additionalData);
requestedItemsCache.push(additionalData);
requestedItemsCache.push(additionalData);