The CreatorCon Call for Content is officially open! Get started here.

why duplicate values storing into Array, How to control it

Supriya25
Tera Guru

Hi All ,

Kindly let me know how to control duplicate records

 

Script include : test 

test2:function(){
var _strXML = '<results probe_time="1633" result_code="0"> <result> <output> [{"number":"12345" ,"status":"A" , "pcstatus":"DRILL" , "op":"654"} , {"number":"5678" ,"status":"P","pcstatus":"DL" , "op":"980"}] </output> </result></results>';

var objXml = new global.XMLDocument(_strXML);
var arrResults = JSON.parse(_objXml.getNodeText('//output').trim());
var obj ={ }; 
var arr =[ ];
for(var i=0;i<arrResults.length;i++){
obj.number = arrResults[i].number.toString();
obj.status = arrResults[i].status.toString();
arr.push(obj);
}
return JSON.stringify(arr);
},
---------------------------------------------------------------------------------------------------------------------
background script : 
gs.info(new test().test2());
output : [{"number":"12345" ,"status":"A" } , {"number":"12345" ,"status":"A" }]

 

expected output 

output : [{"number":"12345" ,"status":"A" } , {"number":"5678" ,"status":"P" }]

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @Supriya25 ,

 

The issue is with the object that you are using- In your script, you were using the same object over and over again, just changing the details (properties) on the same instead of using a new object for each new set of details.
Please try the below code-

var test2 = function() {
    var _strXML = '<results probe_time="1633" result_code="0"><result><output>[{"number":"12345","status":"A","pcstatus":"DRILL","op":"654"},{"number":"5678","status":"P","pcstatus":"DL","op":"980"}]</output></result></results>';

    var objXml = new global.XMLDocument(_strXML);
    var outputText = objXml.getNodeText('//output').trim();

    var arrResults = JSON.parse(outputText);
    var arr = [];
    for (var i = 0; i < arrResults.length; i++) {
        var obj = {
            number: arrResults[i].number.toString(),
            status: arrResults[i].status.toString()
        };
        arr.push(obj);
    }
    var jsonString = JSON.stringify(arr);
    return jsonString;
};

// Execute function in background script
    gs.info(test2());

  

The solution is to use a new object each time you want to store a new set of details. This way, each object holds unique information.

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar

 

 

View solution in original post

7 REPLIES 7

This will work just fine.

Community Alums
Not applicable

Hi @Supriya25 ,

In the code at the end of the loop, every element in the array arr will have the same values, specifically the values assigned in the last iteration of the loop.

To fix this, you should create a new object inside the loop on each iteration. By this way, each object gets its own separate space, and modifying one will not affect the others:

Make the change like the below-

var arr = [];
for (var i = 0; i < arrResults.length; i++) {
    var obj = {}; // Move this inside the loop
    obj.number = arrResults[i].number.toString();
    obj.status = arrResults[i].status.toString();
    arr.push(obj);
}

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar

Sandeep Rajput
Tera Patron
Tera Patron

@Supriya25 Instead of declaring the var obj = {} outside the for loop, you should declare it inside the loop. The following should just work fine without any duplicity.

 

test2:function(){
var _strXML = '<results probe_time="1633" result_code="0"> <result> <output> [{"number":"12345" ,"status":"A" , "pcstatus":"DRILL" , "op":"654"} , {"number":"5678" ,"status":"P","pcstatus":"DL" , "op":"980"}] </output> </result></results>';

var _objXml = new global.XMLDocument(_strXML);

var arrResults = JSON.parse(_objXml.getNodeText('//output').trim());

var arr =[ ];
for(var i=0;i<arrResults.length;i++){
var obj ={ }; //Declated inside the loop to avoid duplicity.
obj.number = arrResults[i].number.toString();
obj.status = arrResults[i].status.toString();
arr.push(obj);
}
return JSON.stringify(arr);
}

Hope this helps.