0
const a = [{
    "iso2": "KH",
    "countryName": "Cambodia",
    "nationality": "Cambodian"
},
{
    "iso2": "KI",
    "countryName": "Kiribati",
    "nationality": "I-Kiribati"
},
{
    "iso2": "KM",
    "countryName": "Comoros",
    "nationality": "Comoran, Comorian"
},
{
    "iso2": "KN",
    "countryName": "Saint Kitts and Nevis",
    "nationality": "Kittitian or Nevisian"
}];

i have an array from Get Api list and i intended to move isow = KN to be on top of the list to be sync with select list option. i know about push and unshift method. but is there is another easiest way to move an object in Array to be on top of the list?

3
  • There's also splice(), which lets you insert and remove anywhere in the list. But if it's always first and last, use push, pop, shift, and unshift. Commented Jan 16, 2020 at 7:40
  • do you want the other items to retain their order? Commented Jan 16, 2020 at 7:49
  • if the object is in the mid i could use splice? and if i want it to be on top i need to shift or push? Commented Jan 16, 2020 at 7:49

4 Answers 4

1

You dont need to sort the complete array if you just want to move the element to the start and do not care about the order of other elements.

You can simply find the index of the element and swap it with the first one.

const a = [{
	"iso2": "KH",
	"countryName": "Cambodia",
	"nationality": "Cambodian"
},
{
	"iso2": "KI",
	"countryName": "Kiribati",
	"nationality": "I-Kiribati"
},
{
	"iso2": "KM",
	"countryName": "Comoros",
	"nationality": "Comoran, Comorian"
},
{
	"iso2": "KN",
	"countryName": "Saint Kitts and Nevis",
	"nationality": "Kittitian or Nevisian"
}];

let index = a.findIndex(e => e.iso2 === 'KN');

a[0] = (_=a[index], a[index] = a[0],_);
console.log(a)

Or can remove the element using Array.splice and then unshift.

const a = [{
	"iso2": "KH",
	"countryName": "Cambodia",
	"nationality": "Cambodian"
},
{
	"iso2": "KI",
	"countryName": "Kiribati",
	"nationality": "I-Kiribati"
},
{
	"iso2": "KN",
	"countryName": "Saint Kitts and Nevis",
	"nationality": "Kittitian or Nevisian"
},
{
	"iso2": "KM",
	"countryName": "Comoros",
	"nationality": "Comoran, Comorian"
}];

let index = a.findIndex(e => e.iso2 === 'KN');
e = a.splice(index,1);
a.unshift(...e)
console.log(a)

Sign up to request clarification or add additional context in comments.

Comments

0

You can use Array.sort to re-order the array as you wish.

const a = [{
    "iso2": "KH",
    "countryName": "Cambodia",
    "nationality": "Cambodian"
  },
  {
    "iso2": "KI",
    "countryName": "Kiribati",
    "nationality": "I-Kiribati"
  },
  {
    "iso2": "KM",
    "countryName": "Comoros",
    "nationality": "Comoran, Comorian"
  },
  {
    "iso2": "KN",
    "countryName": "Saint Kitts and Nevis",
    "nationality": "Kittitian or Nevisian"
  }
];

a.sort((item1) => item1.iso2 === 'KN' ? -1 : 1);

console.log(a);

4 Comments

sorry, but actually the are more than just this data. and the kn is actually in the mid of data.
all country name and iso in world
And you want to move only 'KN' to the start?
I've changed my answer
0

try this:

 let data = [
      {
        iso2: "KH",
        countryName: "Cambodia",
        nationality: "Cambodian"
      },
      {
        iso2: "KI",
        countryName: "Kiribati",
        nationality: "I-Kiribati"
      },
      {
        iso2: "KM",
        countryName: "Comoros",
        nationality: "Comoran, Comorian"
      },
      {
        iso2: "KN",
        countryName: "Saint Kitts and Nevis",
        nationality: "Kittitian or Nevisian"
      }
    ];

    let one = "KN";
    data.sort((x, y) => (x.iso2 == one ? -1 : y.iso2 == one ? 1 : 0));
    console.log(data);

Comments

0

You can simply reverse your array if you want to last object on top. a.reverse();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.