-1

If I Convert my All objects in JSON form Then showing the array "Unexpected script " soo can u tell me if i have a Unwanted object in the given array then how can i filters this particular object not using key and value of the object its possible to filter the particular object here my code :-

 var arr = [
  '{"Topic":"TRACK","SID":"1","GPSlat":"634312740","GPSlong":"104014684","GPSat":"43788","GPSspd":"20","GPShead":"29499222","GPSepoch":"1638451303"}',
  '{"Topic":"TRACK","SID":"1","GPSlat":"634312740","GPSlong":"104014684","GPSalt":"43788","GPSspd":"20","GPShead":"29499222","GPSepoch":"1638451303"}',
   "{'143567890'}",
  '{"Topic":"TRACK","SID":"1","GPSlat":"634312740","GPSlong":"104014684","GPSalt":"43788","GPSspd":"20","GPShead":"29499222","GPSepoch":"1638451303"}',
  '{"Topic":"Impact","SID":"1","GPSlat":"634312740","GPSlong":"104014684","GPSalt":"43788","GPSspd":"20","GPShead":"29499222","GPSepoch":"1638451303"}'
 
];

my code:-

let output = [];
for (let i = 0; i < arr.length; i++)
{
  var ReplaceData = arr[i].replace(/\\/g,''); 
  var Finaldata=JSON.parse(ReplaceData);
 
  console.log
   (Finaldata.GPSlat,Finaldata.GPSlong,Finaldata.SID,"11111111111",Finaldata);
}

my output:-

"634312740", "104014684", "1", "11111111111", {
  GPSat: "43788",
  GPSepoch: "1638451303",
  GPShead: "29499222",
  GPSlat: "634312740",
  GPSlong: "104014684",
  GPSspd: "20",
  SID: "1",
  Topic: "TRACK"
}
"634312740", "104014684", "1", "11111111111", {
  GPSalt: "43788",
  GPSepoch: "1638451303",
  GPShead: "29499222",
  GPSlat: "634312740",
  GPSlong: "104014684",
  GPSspd: "20",
  SID: "1",
  Topic: "TRACK"
}
"Script error."

showing the error and stop my loop soo how can i fixed this error ..

  • @RickyMo i have a json object soo first when i try to convert then showing the error if i try to filter then say try to convert this object into JSON and then filter soo please see my question clearly – virendra kumawat Dec 20 '21 at 09:12
  • Please ask your question clearly. If your code throws error, please post the error message and the stack trace so that people know which line of code causes the error. – Ricky Mo Dec 20 '21 at 10:00

1 Answers1

0

You didn't even state your criteria of filtering. If I have to guess I guess you want to filter out the third element, which is not a valid JSON. You can wrap JSON.parse() in a try/catch block to silent the JSON parse error, return a null then filter it out.

const arr = [
  '{"Topic":"TRACK","SID":"1","GPSlat":"634312740","GPSlong":"104014684","GPSat":"43788","GPSspd":"20","GPShead":"29499222","GPSepoch":"1638451303"}',
  '{"Topic":"TRACK","SID":"1","GPSlat":"634312740","GPSlong":"104014684","GPSalt":"43788","GPSspd":"20","GPShead":"29499222","GPSepoch":"1638451303"}',
   "{'143567890'}",
  '{"Topic":"TRACK","SID":"1","GPSlat":"634312740","GPSlong":"104014684","GPSalt":"43788","GPSspd":"20","GPShead":"29499222","GPSepoch":"1638451303"}',
  '{"Topic":"Impact","SID":"1","GPSlat":"634312740","GPSlong":"104014684","GPSalt":"43788","GPSspd":"20","GPShead":"29499222","GPSepoch":"1638451303"}'
 
];

const output = arr.map( it => {
  try{
    return JSON.parse(it)
  }
  catch(e){
    return null
  }
}).filter(it => !!it);
console.log(output);
Ricky Mo
  • 6,285
  • 1
  • 14
  • 30