4

I have this array:

var itemList = [
    {
        image: "images/home.jpg",
        name: "Home"
    },
    {
        name: "Elvis",
    },
    {
        name: "Jonh"
    },
    {
        image: "images/noah.jpg",
        name: "Noah"
    },
    {
        name: "Turtle"
    }
]

How can I organize the array to objects with image property come first, so that it looks like this?:

var itemList = [
    {
        image: "images/home.jpg",
        name: "Home"
    },
    {
        image: "images/noah.jpg",
        name: "Noah"
    },
    {
        name: "Elvis",
    },
    {
        name: "Jonh"
    },
    {
        name: "Turtle"
    }
]
5
  • 1
    possible duplicate of Sort array of objects by string property value in JavaScript Commented Aug 28, 2015 at 13:36
  • @Andreas No, I need to check if the property exists and not order them. Commented Aug 28, 2015 at 13:37
  • 2
    Are those names correct? you have image, imagem, name and nome? Commented Aug 28, 2015 at 13:41
  • @NicholasRobinson Wops, corrected... Commented Aug 28, 2015 at 13:42
  • Possible duplicate of howto sort an array with objects by property value length?, but your question was unclear, since the accepted answer actually doesn't check length at all and will order the elements with an image property in the order they show up in the array. Commented Feb 12, 2019 at 19:05

3 Answers 3

5

This code put at the beginning elements that have the property 'image'. Other elements stay in the same order.

function compare(a,b) {
   if ('image' in a) {
       return 1;
   } else if ('image' in b) {
      return -1;
   } else {
      return 0;
   }
}

itemList.sort(compare);
Sign up to request clarification or add additional context in comments.

2 Comments

Just check, if image is in both the function needs to return 0.
If image is in both return 1 guarantees that the order stay the same. Returning 0 can change the order between this two elements.
1

Try this:

function compare(a,b) {
  if (a.image && b.image)
    return 0;
  if (a.image)
    return 1;
  return -1;
}

objs.sort(compare);

2 Comments

This is not the situation, but a.image doesn't return true if the property exists, but if the property exists and is value can be evaluated to true. For example if the property exists and the string is null the value is false, if the property is a boolean and the value is false it returns false
The Ecma said: about boolean conversion of strings: "Return false if argument is the empty String (its length is zero); otherwise return true." Similarly null values are converted to false.
0

A bit late, but an alternative:

itemList.sort(function(e1,e2){ return  (e1.image === undefined) - (e2.image === undefined); });

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.