1

I need to move four items from an array of items to the first four positions.

Let say I have the below array:

var countryList=['Afghanistan','Albania', 'Argentina', 'Australia', 'Bangladesh', 'Belgium','Brazil',...]

I need to move Argentina, Australia, Belgium, and Brazil at first four positions. The modified array should look like:

var modifiedCountryList=['Argentina', 'Australia','Belgium', 'Brazil', 'Afghanistan', 'Albania', 'Bangladesh', ...]

I have found an answer here but it is for a single array element position move.

It seems like simple but I can not wrap my head around it.

31piy
  • 23,323
  • 6
  • 47
  • 67
Shahid
  • 21
  • 4

5 Answers5

2

You can array#concat both of your arrays. Using Set get the elements and place them in an array.

NOTE: Use of Set will remove the duplicate element, if any, in the result array.

const countryList=['Afghanistan','Albania', 'Argentina', 'Australia', 'Bangladesh', 'Belgium','Brazil'],
    first = ['Argentina', 'Australia','Belgium', 'Brazil'],
    result = [...new Set([].concat(first, countryList))];
console.log(result);
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
1

Just make a new array with the desired countries in first and .filter out the matching elements in the first array:

const countryList1 = ['Afghanistan','Albania', 'Argentina', 'Australia', 'Bangladesh', 'Belgium','Brazil'];
const countriesInFirst = ['Argentina', 'Australia','Belgium', 'Brazil'];

const countryList2 = [
  ...countriesInFirst,
  ...countryList1.filter(country => !countriesInFirst.includes(country))
];
console.log(countryList2);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
1

var countryList=['Afghanistan','Albania', 'Argentina', 'Australia', 'Bangladesh', 'Belgium','Brazil'];

var filterArray = ["Argentina","Australia","Belgium","Brazil"];

var result = [...filterArray,...countryList].filter((v, i, a) => a.indexOf(v) === i);

console.log(result);
Nishant Dixit
  • 5,388
  • 5
  • 17
  • 29
1

Simply, exclude the first countries from the list of countries and then concatenate the filtered list to the first countries' list:

const countries = ['Afghanistan','Albania', 'Argentina', 'Australia', 'Bangladesh', 'Belgium','Brazil'];
const first = ['Argentina', 'Australia','Belgium', 'Brazil'];

// Get the list of countries without first four countries
const filtered = countries.filter(country => !first.includes(country));

// Prepare the final list of countries
const c1 = first.concat(filtered);
console.log(c1);
31piy
  • 23,323
  • 6
  • 47
  • 67
1

Make a new array with the output you expect. Also, I would suggest to use indexOf instead of includes as includes will give you error in IE browser. You can also make use of spread syntax ... that will add the array elements to your new array.

const countryList1 = ['Afghanistan','Albania', 'Argentina', 'Australia', 'Bangladesh', 'Belgium','Brazil'];
const countriesInFirst = ['Argentina', 'Australia','Belgium', 'Brazil'];

const countryList2 = [
  ...countriesInFirst,
  ...countryList1.filter(country => countriesInFirst.indexOf(country) === -1)
];
console.log(countryList2);

includes() is not supported in IE browser. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes#Browser_compatibility

RobG
  • 142,382
  • 31
  • 172
  • 209
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62