Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

126
Visualizações
Iterate over object with some keys and array of objects inside

Assuming I have this data format:

const matchedProperty = "property1"

const jsonObject = {
    "property1": {
        param1: '',
        param2: '',
        param3: '',
        data: [
            {key: value},
            {key: value},
            {key: value},
            {key: value}
        ],
        param4: ''
    },
    "propery2": {
        param1: '',
        param2: '',
        param3: '',
        data: [
            {key: value},
            {key: value},
            {key: value},
            {key: value}
        ],
        param4: ''
    }

}

How can I check if the property exists inside this json and after that how can I extract only data[{}] and param1 for example?

Can't think of something that would be helpful and working

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Title doesn't match with body, so here's iteration and direct access:

Iterating over Objects: for..in

let object = { a: 1, b: 2, c: 3 }

for (let key in object) {
  console.log(key + ' = ' + object[key])
}

// a = 1
// b = 2
// c = 3

Iterating over Arrays: for..of

let vector = ['a', 'b', 'c']

for (let element of vector) {
  console.log(element)
}

// a
// b
// c

Accessing Array Values At Position

let vector = ['a', 'b', 'c']
console.log(vector[0]) // a
console.log(vector[1]) // b
console.log(vector[2]) // c
console.log(vector[3]) // undefined

Accessing Object Values By Key

let object = { a: 1, b: 2, c: 3 }
console.log(object["a"]) // 1
console.log(object.a)    // 1
console.log(object["b"]) // 2
console.log(object.b)    // 2
console.log(object["c"]) // 3
console.log(object.c)    // 3
console.log(object["d"]) // undefined
console.log(object.d)    // undefined

Check if Exists

Since undefined won't trigger on an if-condition but values will, just write if object["key"]; it will execute the next code block if the key exists, otherwise it won't. (Not suggested practice.)

about 4 years ago · Juan Pablo Isaza Relatório

0

something like this?

const matchedProperty = "property1"

const jsonObject = {
    "property1": {
        param1: '',
        param2: '',
        param3: '',
        data: [
            {key: 'value'},
            {key: 'value'},
            {key: 'value'},
            {key: 'value'}
        ],
        param4: ''
    },
    "propery2": {
        param1: '',
        param2: '',
        param3: '',
        data: [
            {key: 'value'},
            {key: 'value'},
            {key: 'value'},
            {key: 'value'}
        ],
        param4: ''
    }

}

const result = jsonObject.hasOwnProperty(matchedProperty)?{param1: jsonObject[matchedProperty].param1, data: jsonObject[matchedProperty].data}:{}
console.log(result)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda