flix
flix

Reputation: 1984

How to reposition JSON structure key and value?

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?

Upvotes: 2

Views: 102

Answers (5)

Faly
Faly

Reputation: 13356

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);

Upvotes: 1

Aswin Ramesh
Aswin Ramesh

Reputation: 1674

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)

Upvotes: 0

Hassan Imam
Hassan Imam

Reputation: 22574

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);

Upvotes: 1

Justinas
Justinas

Reputation: 43499

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);

Upvotes: 1

Nozar Safari
Nozar Safari

Reputation: 505

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


})

Upvotes: 0

Related Questions