1

I am serializing a form and trying to change some values in this object but it never changes. So I used a test variable for check this and when I am monitoring, this isnt changes too.

 var invoice = _$form.serializeFormToObject();
        var test = { ContainerNumber: "TEST" };
        var collectedContainerNumbers = JSON.parse(invoice.CollectedContainerNumbers);//["ABCD 123456-7","DEFK 231120-0","DNME 222222-2","DNME 321321-3","MACA 010220-2","OKLS 121212-1","TEST 000000-0","TeSt 123456-1","TeSt 123456-6","TeSt 123456-7","TEST 131313-1","TEST 150150-0","TEST 181818-1","TEST 222222-7","TEST 231114-1","TEST 232223-3","TEST 245680-2","TEST 333333-3","TEST 357913-5","TEST 444444-4","TEST 828282-8","TEST 999999-9"] 
        $.each(collectedContainerNumbers, function (index, value) {
            invoice.LogisticFileId = '';
            test.ContainerNumber = value;
            console.log(test);
        });

It is a console result. I am seeing a thing like a title and it is changing but content isnt changing.

console result

It looks like very simple but I don't understand the mistake that I am making.

FSDford
  • 448
  • 5
  • 10
Ender Kaya
  • 138
  • 11
  • Hi @Ender Kaya open this link and I think it will solve your query https://stackoverflow.com/questions/45318501/how-to-update-json-object-value-dynamically-in-jquery – Rajender Kumar Dec 21 '20 at 05:33
  • hi @RajenderKumar it is not working for me because serialized form object is not using with forEach method. – Ender Kaya Dec 21 '20 at 05:47
  • You mean something like [this](https://jsfiddle.net/a4bv5yzd/) ? – Swati Dec 21 '20 at 06:06
  • @Swati same but little difference. i mean like [this](https://jsfiddle.net/kdb5cqzt/) I want to use all values in array but original code isnt simple like that. invoice object has a lot of properties. I want to change just one of them and post to other method. – Ender Kaya Dec 21 '20 at 06:14
  • can you update your question with excepted output ? Also, proper json structure . – Swati Dec 21 '20 at 06:19
  • Please check this answer https://stackoverflow.com/a/62548305/9695286, you might be able to understand why console is showing such results but your actual object would be correct. – Karan Dec 21 '20 at 06:28
  • @Karan it is not related with this i think because my actual object isnt correcy but thank you. – Ender Kaya Dec 21 '20 at 06:35
  • Try with `console.log(JSON.stringify(test))` & check check what output you got on console. – Karan Dec 21 '20 at 06:37

1 Answers1

1

Edit you will need an array instead of object to send all values to backend. Try converting your test object as an array like below.

var invoice = _$form.serializeFormToObject();
var test = []; // declare test as an array.
collectedContainerNumbers = JSON.parse(invoice.CollectedContainerNumbers);
$.each(collectedContainerNumbers, function (index, value) {
    invoice.LogisticFileId = '';
    test.push({ContainerNumber = value}); // push value in an array
    console.log(test);
});

Try this thing in console it might solve your confusion. Add an object like let test = { ContainerNumber: 'ABCD 123456-7' }. Then log it in console. Do not expand right now. Now change the value of test.ContainerNumber = 'TEST 000000-0';. Now log the object & expand both logs. In first log also when you expand it will display { ContainerNumber = 'TEST 000000-0' }. as it is fetching current values when we expand.

You can also log ContainerNumber and check what exactly values hold by test.ContainerNumber. Like in below snippet. It will show you when you do console.log(test.ContainerNumber); first time it will show ABCD 123456-7 and second time it will show TEST 000000-0 which is as expected.

Try with below like. 1. Open console of browser. 2. Click on Run Code Snippet. 3. Expand object in console.

let test = { ContainerNumber: 'ABCD 123456-7' };
console.log(test);
console.log(test.ContainerNumber); // Output "ABCD 123456-7"

test.ContainerNumber = 'TEST 000000-0';

console.log(test);
console.log(test.ContainerNumber); // Output "TEST 000000-0"
Check browser console.
Karan
  • 12,059
  • 3
  • 24
  • 40
  • you can be right but when i am posting object to backend, i am seeing same value everytime. how can i use different value? – Ender Kaya Dec 21 '20 at 07:06