Shahid
Shahid

Reputation: 21

Move multiple array elements from one array position to another

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.

Upvotes: 1

Views: 1311

Answers (5)

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

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

Upvotes: 1

Hassan Imam
Hassan Imam

Reputation: 22574

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);

Upvotes: 2

31piy
31piy

Reputation: 23869

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);

Upvotes: 1

Nishant Dixit
Nishant Dixit

Reputation: 5522

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);

Upvotes: 1

CertainPerformance
CertainPerformance

Reputation: 371138

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);

Upvotes: 1

Related Questions