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

163
Views
How to reorder this array without an index?

Let say I have

let arr = [
{code: 5, country: 'DE', price: 8},
{code: 6, country: 'RU', price: 9},
{code: 7, country: 'US', price: 10},
{code: "FiveX", country: 'UK', price: 11},
{code: 5, country: 'ES', price: 12},
{code: 5, country: 'RU', price: 13},
{code: 7, country: 'BE', price: 14},
]

I need this array to be reordered as follows

arr = [
{priceDE: '8 (code5)' , priceES: '12 (code5)', priceRU: '13 (code5)'},
{priceRU: '9 (code6)'},
{priceUS: '10 (code7)', priceBE: '14 (code7)'}
{priceUK: '11 (codeFiveX)'}
]

I basically need to group them with the code reference, but I dont really understand how to do it without knowing the value of code beforehand. Keep in mind that code isn't always a number it can be a string

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

First of all your original array declaration is wrong, as it uses DE as a variable while it should be a string encapsulated by quotes 'DE' so you need to rewrite that to be reproducible.

So for example {code: 5, country: 'DE', price: 8},

Then you need to loop your original array, take the values and rebuild a new object with the new values. I say object and not array since we need to use a key that in your case can be either a number, e.g. 5, or a string, e.g. FiveX.

Here's the resulting snippet

let arr = [
{code: 5, country: 'DE', price: 8},
{code: 6, country: 'RU', price: 9},
{code: 7, country: 'US', price: 10},
{code: "FiveX", country: 'UK', price: 11},
{code: 5, country: 'ES', price: 12},
{code: 5, country: 'RU', price: 13},
{code: 7, country: 'BE', price: 14},
]

let new_arr = {}
for(i=0; i<arr.length; i++) {
    let code = arr[i].code
    let country = arr[i].country
    let price = arr[i].price
    if (code in new_arr) {
        new_arr[code]['price'+country] = price
    } else {
        new_arr[code] = {}
        new_arr[code]['price'+country] = price
    }
}
console.log(new_arr)

The resulting array will be what you requested, apart from the weird (code5) which if you want you can add by concatenating price+" (code"+code+")" in the for loop.

EDIT

I forgot, if you want the final item to be an array instead of an object, you can loop it again.

let final_arr = []
for(i in new_arr) {
    final_arr.push(new_arr[i])
}
console.log(final_arr)
about 4 years ago · Juan Pablo Isaza 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!