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

Bert_c1
Kilo Patron

Incorporate the 'ArrayUtil' script include and use the 'unique(arr)' function.

 

background script : 
gs.info(new test().test2());
output : [{"number":"12345" ,"status":"A" } , {"number":"12345" ,"status":"A" }]

var uniqueArr = new ArrayUtil().unique(new test().test2());
var resul = JSON.parse(uniqueArr);
for(var i=0;i<resul.length;i++)
{
gs.info('Account number :"+resul[i].number.toString())
}

 

 

I tried below script, but expected results not coming

 

Error coming : unexpected comma in array literal.

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

 

 

Perfect it is working now. Thanks a lot 

 

Please help me with  one point here 

does this below process will not support ?

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);
}