user1102152
user1102152

Reputation: 61

sort array of objects if the object has a property

I have an object like this:

"data": [
    {
        "name": "name1",
        "price": 4.2,
    },
    {
        "name": "name2",
        "price": 12.9,
        "newOne": {}
    },
    {
        "name": "name3",
        "price": 10.9,
        "newOne": {
            "code": "02ec583021de8e36ae8006c3caef72d9",
            "name": "מבצע 13"
        }
    },
    {
        "name": "name3",
        "price": 10.9,
    },
],

I have tried many ways to sort it...Need some help

If this array has a "newOne" property I Want that object to be first in the array etc'...

If you have 3 objects with the proprty "newOne" so they will be the first to show one after the other and not that the next mapping object will be at the top of the array.

I am using React if that means anything

Thanks!

Upvotes: 1

Views: 325

Answers (4)

Goran.it
Goran.it

Reputation: 6299

Here is another sorting function without using array in:

data.sort((a,b) => a.newOne === b.newOne ? 0 : a.newOne ? -1 : 1)

Upvotes: 0

dgeare
dgeare

Reputation: 2658

This should be doable by treating items with a set newOne property as greater in value than those that don't.

var t = {"data": [
    {
        "name": "name1",
        "price": 4.2,
    },
    {
        "name": "name2",
        "price": 12.9,
        "newOne": {}
    },
    {
        "name": "name3",
        "price": 10.9,
        "newOne": {
            "code": "02ec583021de8e36ae8006c3caef72d9",
            "name": "מבצע 13"
        }
    },
    {
        "name": "name3",
        "price": 10.9,
    },
]};

t.data.sort(function(a,b){
  var aVal = a.newOne == undefined ? 0 : 1;
  var bVal = b.newOne == undefined ? 0 : 1;
  return bVal - aVal;
});

console.log(t.data);

Upvotes: 0

omri_saadon
omri_saadon

Reputation: 10631

You can use the function unshift from array to achieve your goal.

The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.

const data = 
     [
      {
          "name": "name1",
          "price": 4.2,
      },
      {
          "name": "name2",
          "price": 12.9,
          "newOne": {}
      },
      {
          "name": "name3",
          "price": 10.9,
          "newOne": {
              "code": "02ec583021de8e36ae8006c3caef72d9",
              "name": "מבצע 13"
          }
      },
      {
          "name": "name3",
          "price": 10.9,
      },
  ]

let result = []
data.forEach(item => {
  if (item.newOne) result.unshift(item)
  else result.push(item)
})

console.log(result)

Same could be achieved with reduce:

const result = data.reduce((agg, itr) => {
  if (itr.newOne) agg.unshift(itr)
  else agg.push(itr)
  return agg
}, [])

console.log(result)

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386604

You could check for the property and take the check for the delta for sorting.

var data = [{ name: "name1", price: 4.2 }, { name: "name2", price: 12.9, newOne: {} }, { name: "name3", price: 10.9, newOne: { code: "02ec583021de8e36ae8006c3caef72d9", name: "מבצע 13" } }, { name: "name3", price: 10.9 }];

data.sort((a, b) => ('newOne' in b) - ('newOne' in a));

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

Upvotes: 5

Related Questions