0

Using NodeJS

I need to take a json array like below.

    const data = [
  {
    pid: 40944,
    city: 'Port Melbourne',
    region1: 'Victoria',
    region2: ''
  },
  {
    pid: 40785,
    city: 'Melbourne',
    region1: 'Victoria',
    region2: ''
  },
  {
    pid: 40786,
    city: 'West Melbourne',
    region1: 'Victoria',
    region2: ''
  },
  {
    pid: 40722,
    city: 'Melbourne',
    region1: 'Victoria',
    region2: ''
  },
  {
    pid: 40788,
    city: 'North Melbourne',
    region1: 'Victoria',
    region2: ''
  }
]

And change the structure, remove duplicates (suburb) & order A-Z (suburb).

{
  '40785': 'Melbourne, Victoria',
  '40788': 'North Melbourne, Victoria',
  '40944': 'Port Melbourne, Victoria',
  '40786': 'West Melbourne, Victoria',
}

I've been able to create the structure using the following but I still need to remove duplicates and sort A-Z

    const location = Object.fromEntries(rows.map((data) => [
        data.pid,
        data.region2 ? `${data.city}, ${data.region1}, ${data.region2}` : `${data.city}, ${data.region1}`
    ]))

thankyou!

Adam
  • 19,932
  • 36
  • 124
  • 207
  • 1
    Regarding sorting, you could do it before transforming the array into an object using the `.sort()` method. Take a look at this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort – Firmino Changani Feb 25 '21 at 11:09
  • you won't be able to sort your object like that. The keys will need to go in ascending order for numeric (array) keys. Don't rely on object key-ordering, instead, make an array of keys and order that. – Nick Parsons Feb 25 '21 at 11:40

2 Answers2

1
const citiesObj = {}

data.forEach( city => {
  citiesObj[city.city] = city;
})

You can use this. It will give you object filtered by city name which will be sorted.

const data = [
  {
    pid: 40944,
    city: 'Port Melbourne',
    region1: 'Victoria',
    region2: ''
  },
  {
    pid: 40785,
    city: 'Melbourne',
    region1: 'Victoria',
    region2: ''
  },
  {
    pid: 40786,
    city: 'West Melbourne',
    region1: 'Victoria',
    region2: ''
  },
  {
    pid: 40722,
    city: 'Melbourne',
    region1: 'Victoria',
    region2: ''
  },
  {
    pid: 40788,
    city: 'North Melbourne',
    region1: 'Victoria',
    region2: ''
  }
]

const citiesObj = {}

data.forEach( city => {
  citiesObj[city.city] = city;
})

console.log('citiesObj', citiesObj)
Sandip Nirmal
  • 2,283
  • 21
  • 24
1

The strategy I used is to first of all remove the duplicates, then sort the non-redundant result.

const data = [{
    pid: 40944,
    city: 'Port Melbourne',
    region1: 'Victoria',
    region2: ''
  },
  {
    pid: 40785,
    city: 'Melbourne',
    region1: 'Victoria',
    region2: ''
  },
  {
    pid: 40786,
    city: 'West Melbourne',
    region1: 'Victoria',
    region2: ''
  },
  {
    pid: 40722,
    city: 'Melbourne',
    region1: 'Victoria',
    region2: ''
  },
  {
    pid: 40788,
    city: 'North Melbourne',
    region1: 'Victoria',
    region2: ''
  }
]

let city = {};
let result = {};
data.forEach((el) => {
  let elemLowerCaseCity = el.city.toLowerCase();
    
  if (!city[elemLowerCaseCity]) {
    city[elemLowerCaseCity] = el;
        result[el.pid] = el.city + ', ' + el.region1;
  }
})

console.log({result}, {city})

As for the final output, displaying objects in a sorted manner is not possible in Javascript. Refer to this question, and this for a much cleaner answer question. But I would advice you to use arrays to hold your data. Take a look at the city object in my code block. I hope I was able to help you out?