2

I have a json like this:

[
    {
        "tipe": "foo1",
        "color": "red",
        "size": 92,
        "seq": 1,
        "hasil": 0
    },
    {
        "tipe": "foo2",
        "color": "red",
        "size": 92,
        "seq": 2,
        "hasil": 1
    },
    {
        "tipe": "foo3",
        "color": "red",
        "size": 92,
        "seq": 3,
        "hasil": 0
    }
]

I want to reposition the structure of json in case hasil = 1 into last position, and keep the seq value,

I've reasearch before asking here, unfortunately I don't know the right keyword of this issue.

purpose:

[
    {
        "tipe": "foo1",
        "color": "red",
        "size": 92,
        "seq": 1,
        "hasil": 0
    },
    {
        "tipe": "foo3",
        "color": "red",
        "size": 92,
        "seq": 2,
        "hasil": 0
    },
    {
        "tipe": "foo2",
        "color": "red",
        "size": 92,
        "seq": 3,
        "hasil": 1
    }
]

there's a way to make it possible?

flix
  • 1,688
  • 3
  • 34
  • 64

5 Answers5

1

Simply sort your array with user function

var array = [
    {
        "tipe": "foo1",
        "color": "red",
        "size": 92,
        "seq": 1,
        "hasil": 0
    },
    {
        "tipe": "foo2",
        "color": "red",
        "size": 92,
        "seq": 2,
        "hasil": 1
    },
    {
        "tipe": "foo3",
        "color": "red",
        "size": 92,
        "seq": 3,
        "hasil": 0
    }
];

array.sort(function (a, b) {
  return a.hasil - b.hasil;
});

for (var i = 0; i < array.length; i++) {
  array[i].seq = i+1;
}

console.log(array);
Justinas
  • 41,402
  • 5
  • 66
  • 96
1

Sort your array by hasil then loop through it to reassign seq:

var arr = [
    {
        "tipe": "foo1",
        "color": "red",
        "size": 92,
        "seq": 1,
        "hasil": 0
    },
    {
        "tipe": "foo2",
        "color": "red",
        "size": 92,
        "seq": 2,
        "hasil": 1
    },
    {
        "tipe": "foo3",
        "color": "red",
        "size": 92,
        "seq": 3,
        "hasil": 0
    }
];
   
var seqs = arr.map(e => e.seq);
arr.sort((a, b) => a.hasil - b.hasil).forEach((e, i) => e.seq = seqs[i]);
console.log(arr);
Faly
  • 13,291
  • 2
  • 19
  • 37
1

You can create an array of hasil values and sort it and using array#map and object#assign create your new array.

var input = [{ "tipe": "foo1", "color": "red", "size": 92, "seq": 1, "hasil": 0 }, { "tipe": "foo2", "color": "red", "size": 92, "seq": 2, "hasil": 1 }, { "tipe": "foo3", "color": "red", "size": 92, "seq": 3, "hasil": 0 }],
    sortedHasil = input.map(({hasil}) => hasil).sort(),
    result = input.map((o, i) => Object.assign(o, {'hasil': sortedHasil[i]}));
console.log(result);
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
0

try sort function if u dont want to sort u can do it with if else

var input =[
    {
        "tipe": "foo1",
        "color": "red",
        "size": 92,
        "seq": 1,
        "hasil": 0
    },
    {
        "tipe": "foo3",
        "color": "red",
        "size": 92,
        "seq": 2,
        "hasil": 0
    },
    {
        "tipe": "foo2",
        "color": "red",
        "size": 92,
        "seq": 3,
        "hasil": 1
    }
]

input.sort(function (a,b){

    return a.hasil - b.hasil


})
Nozar Safari
  • 505
  • 4
  • 17
0

you can have a sort and then a map function to handle this, like

var json = [
    {
        "tipe": "foo1",
        "color": "red",
        "size": 92,
        "seq": 1,
        "hasil": 0
    },
    {
        "tipe": "foo2",
        "color": "red",
        "size": 92,
        "seq": 2,
        "hasil": 1
    },
    {
        "tipe": "foo3",
        "color": "red",
        "size": 92,
        "seq": 3,
        "hasil": 0
    }
]


var newArr = json.sort(function(a, b){
  if(a.hasil === b.hasil)
      return a.seq  - b.seq;

  return a.hasil - b.hasil
}).map(function(a, i) {
  return {
    ...a,
    seq: i+1
  }
})

console.log(newArr)
Aswin Ramesh
  • 1,654
  • 1
  • 13
  • 13