Ruan Duarte
Ruan Duarte

Reputation: 385

Array sorting when there is no value in an object property

I have the following array

[
  {
    "id": 1,
    "name": "Ruan Duarte",
    "idade": 11,
    "work": {
      "id": 2
    }
  },
  {
    "id": 2,
    "name": "Raul Dias",
    "idade": 13
  },
  {
    "id": 7,
    "name": "Caio",
    "idade": 60,
    "work": {
      "id": 4
    }
  },
  {
    "id": 3,
    "name": "Felipe Lima",
    "idade": 55
  },
  {
    "id": 4,
    "name": "Camila",
    "idade": 25,
    "work": {
      "id": 3
    }
  }
]

I have an array in this format, where the work.id field in some corners is null. I try to do the ordering as follows ...

array.sort((a, b) => {
  return (
    a.work.id - b.work.id
  )
})

However, I get an error for the non-existence of the id

Upvotes: 2

Views: 760

Answers (3)

Kettei Sproutty
Kettei Sproutty

Reputation: 51

If you need to sort by work.id:

const arr = [
  {
    "id": 1,
    "name": "Ruan Duarte",
    "idade": 11,
    "work": {
      "id": 2
    }
  },
  {
    "id": 2,
    "name": "Raul Dias",
    "idade": 13
  },
  {
    "id": 7,
    "name": "Caio",
    "idade": 60,
    "work": {
      "id": 4
    }
  },
  {
    "id": 3,
    "name": "Felipe Lima",
    "idade": 55
  },
  {
    "id": 4,
    "name": "Camila",
    "idade": 25,
    "work": {
      "id": 3
    }
  }
]

arr.sort((a, b) => {
    if (a.work && b.work) {
    return a.work.id > b.work.id ? 1 : - 1
  } else if (!a.work && !b.work) {
        return a.id > b.id ? 1 : - 1
    } else if (!a.work) {
        return 1
    } else {
    return - 1
  }
})

output:

[
  {
    "id": 1,
    "name": "Ruan Duarte",
    "idade": 11,
    "work": {
      "id": 2
    }
  },
  {
    "id": 4,
    "name": "Camila",
    "idade": 25,
    "work": {
      "id": 3
    }
  },
  {
    "id": 7,
    "name": "Caio",
    "idade": 60,
    "work": {
      "id": 4
    }
  },
  {
    "id": 2,
    "name": "Raul Dias",
    "idade": 13
  },
  {
    "id": 3,
    "name": "Felipe Lima",
    "idade": 55
  }
]

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386654

You could take optional chaining operator ?. with the properties and the Nullish coalescing operator ?? for taking a default value which respects zero.

By taking Number.MAX_VALUE as default value, all objects with missing nested objects are sorted to bottom.

const
    data = [{ id: 1, name: "Ruan Duarte", idade: 11, work: { id: 2 } }, { id: 2, name: "Raul Dias", idade: 13 }, { id: 7, name: "Caio", idade: 60, work: { id: 4 } }, { id: 3, name: "Felipe Lima", idade: 55 }, { id: 4, name: "Camila", idade: 25, work: { id: 3 } }];

data.sort((a, b) => (a?.work?.id ?? Number.MAX_VALUE) - (b?.work?.id ?? Number.MAX_VALUE));

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 5

Nooruddin Lakhani
Nooruddin Lakhani

Reputation: 6967

This might help

array.sort((a, b) => {
  return (
      (a.work != null ? a.work.id : -Infinity) - (b.work != null ? b.work.id : -Infinity) 
  )
});

Upvotes: 0

Related Questions