Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

124
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!