-1

I have a array with different and unique errors as this:

[
 "ClientError('Bad Request: Not authorized to view user',)",
 "ConnectionResetError(104, 'Connection reset by peer')",
 "CursorNotFound('Cursor not found, cursor id: 195552090717',)",
 "CursorNotFound('Cursor not found, cursor id: 193743299994',)",
 "CursorNotFound('Cursor not found, cursor id: 194974042489',)"
]

I have only 3 errors but since the cursor id is different the functions create more errors. How can group CursorNotFound errors in one group?

I want to get something like this:

[
    "ClientError('Bad Request: Not authorized to view user',)",
    "ConnectionResetError(104, 'Connection reset by peer')",
    "groupA": [
        "CursorNotFound('Cursor not found, cursor id: 195552090717',)",
        "CursorNotFound('Cursor not found, cursor id: 193743299994',)",
        "CursorNotFound('Cursor not found, cursor id: 194974042489',)"
    ]
]

Can indexof serve this?

let allError = [];
 _array.forEach(element => allError.push(element.error));
let uniq = Array.from(new Set(allError));
console.log(uniq);
adiga
  • 34,372
  • 9
  • 61
  • 83
Hernan Humaña
  • 433
  • 5
  • 18
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Liam Nov 26 '18 at 13:52
  • 1
    Just my opinion, not a real answer: there is a lot to improve here. You start with an array of *strings that need to be parsed to extract data from them*, and you want to turn that into an ***array of a subset of the same 'complex' strings along with a sub-array (or multiple?) for the remainder of these 'complex' strings***. Why even keep those strings instead of parsing them first into something meaningful? If you don't, they will be a pain forever. – Peter B Nov 26 '18 at 13:55

1 Answers1

1

Your groupA in your wanted array should be an object or it should be a full string, this should do it but there is better methods

var errors = [
     "ClientError('Bad Request: Not authorized to view user',)",
     "ConnectionResetError(104, 'Connection reset by peer')",
     "CursorNotFound('Cursor not found, cursor id: 195552090717',)",
     "CursorNotFound('Cursor not found, cursor id: 193743299994',)",
     "CursorNotFound('Cursor not found, cursor id: 194974042489',)"
    ]

var cursorNotFound = [];
var newErr = [];
errors.forEach((element) => {
    if (element.includes('CursorNotFound')) {
        cursorNotFound.push(element);
    } else {
        newErr.push(element);
    }
})

newErr.push({
    "groupA": cursorNotFound 
})
// newErr.push("groupA: [" + cursorNotFound + "]")

Result :

[ 'ClientError(\'Bad Request: Not authorized to view user\',)',
  'ConnectionResetError(104, \'Connection reset by peer\')',
  { groupA:
     [ 'CursorNotFound(\'Cursor not found, cursor id: 195552090717\',)',
       'CursorNotFound(\'Cursor not found, cursor id: 193743299994\',)',
       'CursorNotFound(\'Cursor not found, cursor id: 194974042489\',)' ] } ]

Result for full string groupA:

[ 'ClientError(\'Bad Request: Not authorized to view user\',)',
  'ConnectionResetError(104, \'Connection reset by peer\')',
  'groupA: [CursorNotFound(\'Cursor not found, cursor id: 195552090717\',),CursorNotFound(\'Cursor not found, cursor id: 193743299994\',),CursorNotFound(\'Cursor not found, cursor id: 194974042489\',)]' ]
Anouar Kacem
  • 635
  • 10
  • 24