Created
March 11, 2022 16:56
-
-
Save NathanAlcantara/78e924cbbf156de784308280bd7ce2e7 to your computer and use it in GitHub Desktop.
Find object on a complex array by id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example object that contains a complex array (an array with object that has arrays and go on) | |
const json = { | |
type: 'AdaptiveCard', | |
version: '1.4', | |
body: [ | |
{ | |
type: 'Container', | |
items: [ | |
{ | |
type: 'TextBlock', | |
text: 'Checagem da saúde do projeto', | |
wrap: true, | |
separator: true, | |
size: 'Medium', | |
weight: 'Bolder', | |
}, | |
{ | |
type: 'TextBlock', | |
text: 'Informe abaixo o nome do projeto e o nome do grupo que o mesmo pertence para realizar a checagem da saúde do projeto', | |
wrap: true, | |
spacing: 'Medium', | |
color: 'Light', | |
}, | |
], | |
}, | |
{ | |
type: 'ColumnSet', | |
columns: [ | |
{ | |
type: 'Column', | |
width: 'stretch', | |
items: [ | |
{ | |
type: 'Input.Text', | |
id: 'projectName', | |
placeholder: 'ex: sdl', | |
isRequired: true, | |
label: 'Nome do projeto', | |
errorMessage: 'O nome do projeto precisa ser informado', | |
}, | |
], | |
}, | |
{ | |
type: 'Column', | |
width: 'stretch', | |
items: [ | |
{ | |
type: 'Input.Text', | |
placeholder: 'ex: arquitetura', | |
id: 'groupName', | |
isRequired: true, | |
label: 'Nome do grupo', | |
errorMessage: 'O nome do grupo precisa ser informado', | |
}, | |
], | |
}, | |
], | |
spacing: 'Large', | |
}, | |
{ | |
type: 'ActionSet', | |
actions: [ | |
{ | |
type: 'Action.Submit', | |
title: 'Checar', | |
data: { | |
msteams: { | |
type: 'messageBack', | |
text: 'checkProjectHealth', | |
value: 'value', | |
}, | |
}, | |
}, | |
], | |
spacing: 'Medium', | |
}, | |
], | |
}; | |
// Prototyping to use equal than native funtion | |
Array.prototype.findById = function (id) { | |
let element; | |
this.forEach((el) => { | |
if (!element) { | |
if (el.id === id) { | |
element = el; | |
} | |
Object.values(el).forEach((value) => { | |
if (typeof value === 'object' && value.length) { | |
element = value.findById(id); | |
} | |
}); | |
} | |
}); | |
return element; | |
}; | |
// Calling function | |
const element = json.body.findById('projectName'); | |
console.log(element); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment