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
Split object into two and renumerate keys

I would like to split an object into two parts according to property "amount" (empty string)

let myObj = {
"1": {
    "resources": "hotel",
    "amount": "",
    "currency": ""
},
"2": {
    "resources": null,
    "amount": "300.00",
    "currency": "CZK"
},
"3": {
    "resources": null,
    "amount": "500.00",
    "currency": "USD"
},

}

to this

obj1 = {
"1": {
    "resources": "hotel",
    "amount": "",
    "currency": ""
}}
obj2 = {
"1": {
    "resources": null,
    "amount": "300.00",
    "currency": "CZK"
},
"2": {
    "resources": null,
    "amount": "500.00",
    "currency": "USD"
}}

I'm close to solving it but after numerous attempts (push, assign, map) it still does not work. Thx.

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

This is the simplest and most readable solution i have thought of.

const obj = {
    "1": {
        "resources": "hotel",
        "amount": "",
        "currency": ""
    },
    "2": {
        "resources": null,
        "amount": "300.00",
        "currency": "CZK"
    },
    "3": {
        "resources": null,
        "amount": "500.00",
        "currency": "USD"
    }
}

const withAmount = {};
const withoutAmount = {};

for(indexKey in obj) {
  const data = obj[indexKey];
  if(data['amount'] != '') {
    withAmount[indexKey] = data;
  } else {
    withoutAmount[indexKey] = data;
  }
}

console.log({withAmount, withoutAmount});

about 4 years ago · Santiago Gelvez Relatório

0

You can acheive your goal like this:

let myObj = {
  "1": {
    "resources": "hotel",
    "amount": "",
    "currency": ""
  },
  "2": {
    "resources": null,
    "amount": "300.00",
    "currency": "CZK"
  },
  "3": {
    "resources": null,
    "amount": "500.00",
    "currency": "USD"
  },
}

const withAmount = {},
  withoutAmount = {};

Object.keys(myObj).forEach(key => {
  const item = myObj[key];
  if (item.amount) {
    withAmount[key] = item;
  } else {
    withoutAmount[key] = item
  }
})

console.log('withAmount:',withAmount)
console.log('withoutAmount:',withoutAmount)

about 4 years ago · Santiago Gelvez 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