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

96
Visualizações
Assign value to elements in array from Object properties and different Array

Hey I would like to create a new array, from those two elements looking like this:

var a = {szerokosc: 42, wysokosc: 53, dlugosc: null}    // an object
var b = ['dlugosc', '*', 'szerokosc', '+', 'wysokosc']  // an array

The code must work if the correct name is found. At the End I hope to receive an array like this one:

var c = ['0', '*', '42', '+', '53']

If anyone can tell me how to achieve this, I'd appreciate it. Thanks in advance.

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

0

When using a[x] to check the data, simply using b.map(x => a[x] || x) will return [ 'dlugosc', '*', 42, '+', 53 ] due to dlugosc having null value so you need to catch that by checking if the key is present in the object.

Steps:

  1. If the key is present, check if a[x] has a value
  2. If a[x] doesn't have any value, return 0 instead.
  3. If a[x] has a value, return that.
  4. Lastly, if the key isn't present in the object, return the same element

var a = {szerokosc: 42, wysokosc: 53, dlugosc: null}    // an object
var b = ['dlugosc', '*', 'szerokosc', '+', 'wysokosc']  // an array

console.log(b.map(x => Object.keys(a).includes(x) ? !a[x] ? 0 : a[x] : x))

about 4 years ago · Juan Pablo Isaza Relatório

0

you can do this

var a = {szerokosc: 42, wysokosc: 53, dlugosc: 0}    // an object
var b = ['dlugosc', '*', 'szerokosc', '+', 'wysokosc']  // an array

const transform = (data, dictonary) => data.map(i => dictonary.hasOwnProperty(i)? dictonary[i] : i)

console.log(transform(b, a))

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