-2

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

mouchin777
  • 1,428
  • 1
  • 31
  • 59
  • 1
    Start by formatting the expected output. – 0stone0 Mar 28 '22 at 16:17
  • Does this answer your question? [How can I group an array of objects by key?](https://stackoverflow.com/questions/40774697/how-can-i-group-an-array-of-objects-by-key) – The Fool Mar 28 '22 at 16:19
  • Wouldn't you want: `{code: 7, priceUS: 10, priceBE: 14}` or better still `{code: 7, price: {US: 10, BE: 14}}` – jarmod Mar 28 '22 at 16:19
  • no @jarmod thats not valid for my case – mouchin777 Mar 28 '22 at 16:20
  • 1
    I don't understand what you're after. In your example, you're not reordering the elements but (apparently) mapping the original list into some other list. And what do you mean by "how to do it without knowing the value of code beforehand"? In your example, the `code` property is defined in all elements of the list. – Emma Mar 28 '22 at 16:21
  • OK, but what do you mean by `{priceRU: 9 (code6)}`? Specifically, this is not valid JavaScript. – jarmod Mar 28 '22 at 16:24
  • @jarmod "9 (code6)" is just a string – mouchin777 Mar 28 '22 at 16:24
  • This `{code: 5, country: DE, price: 8},` is not valid, unless you have some constants defined for DE somewhere. Did you mean -> `{code: 5, country: 'DE', price: 8},` – Keith Mar 28 '22 at 16:25
  • @Keith yes I meant those as strings – mouchin777 Mar 28 '22 at 16:26

1 Answers1

1

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)
Andrea Olivato
  • 2,450
  • 1
  • 18
  • 30