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

119
Views
Taking Some Element in a Paricular Order In JavaScript

I have an object like the below, i want to map this array in a particular order like, Argentina, Chilean, United States

1: {label: 'Turkish Lira', flag: 'TR', id: 1, code: 'TRY', symbol: '₺', …}
2: {label: 'United States Dollar', flag: 'US', id: 2, code: 'USD', symbol: '$', …}
3: {label: 'Argentina Peso', flag: 'AR', id: 3, code: 'ARS', symbol: '$', …}
4: {label: 'Bahraini Dinar', flag: 'BH', id: 4, code: 'BHD', symbol: ' BD', …}
5: {label: 'Brazilian Real', flag: 'BR', id: 5, code: 'BRL', symbol: ' R$', …}
6: {label: 'Bulgarian Lev', flag: 'BG', id: 6, code: 'BGN', symbol: ' лв', …}
7: {label: 'Chilean Peso', flag: 'CL', id: 7, code: 'CLP', symbol: '$', …}

How can i do that?

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

0

Given you have a list of items you want to sort, called items. You have a list of indexes, such as [0, 4, 2, 1, 3]

You can indexes.map(i => items[i]);

var items = [{
    label: 'Turkish Lira',
    flag: 'TR',
    id: 1,
    code: 'TRY',
    symbol: '₺'
  }, {
    label: 'United States Dollar',
    flag: 'US',
    id: 2,
    code: 'USD',
    symbol: '$'
  },
  {
    label: 'Argentina Peso',
    flag: 'AR',
    id: 3,
    code: 'ARS',
    symbol: '$',
  },
  {
    label: 'Bahraini Dinar',
    flag: 'BH',
    id: 4,
    code: 'BHD',
    symbol: ' BD'
  },
  {
    label: 'Brazilian Real',
    flag: 'BR',
    id: 5,
    code: 'BRL',
    symbol: ' R$'
  },
  {
    label: 'Bulgarian Lev',
    flag: 'BG',
    id: 6,
    code: 'BGN',
    symbol: ' лв'
  },
  {
    label: 'Chilean Peso',
    flag: 'CL',
    id: 7,
    code: 'CLP',
    symbol: '$'
  }
]

var indexesList = [3, 5, 0, 2];

function sortByIndex(list, indexes) {
  return indexes.map(i => items[i]);
}

console.log(sortByIndex(items, indexesList));

about 4 years ago · Juan Pablo Isaza Report

0

You can use javascript array sort

const array = [{
  label: 'Turkish Lira',
  flag: 'TR',
  id: 1,
  code: 'TRY',
  symbol: '₺'
}, {
  label: 'United States Dollar',
  flag: 'US',
  id: 2,
  code: 'USD',
  symbol: '$'
}, {
  label: 'Argentina Peso',
  flag: 'AR',
  id: 3,
  code: 'ARS',
  symbol: '$'
}]

function sortBy(a, b) {
  if (a.label < b.label) {
    return -1;
  }
  if (a.label > b.label) {
    return 1;
  }
  return 0;
}

console.log(array.sort(sortBy));

If you want a generic functionality then you can do this.

const array = [{
  label: 'Turkish Lira',
  flag: 'TR',
  id: 1,
  code: 'TRY',
  symbol: '₺'
}, {
  label: 'United States Dollar',
  flag: 'US',
  id: 2,
  code: 'USD',
  symbol: '$'
}, {
  label: 'Argentina Peso',
  flag: 'AR',
  id: 3,
  code: 'ARS',
  symbol: '$'
}]

const sortBy = (attribute) => (a, b) => {
  if (a[attribute] < b[attribute]) {
    return -1;
  }
  if (a[attribute] > b[attribute]) {
    return 1;
  }
  return 0;
}

console.log(array.sort(sortBy('label')));
console.log(array.sort(sortBy('code')));

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!