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?

3
  • sorting an array. It is what you need. Commented Jan 16, 2018 at 7:11
  • This question seemingly has nothing to do with JSON at all... Commented Jan 16, 2018 at 7:12
  • Try to check this one : stackoverflow.com/questions/17684921/… Commented Jan 16, 2018 at 7:13

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

5 Comments

OP is looking to retain the seq value at the same index.
the problem is about seq, i need to keep seq in the same position as first json,
@flix Check update. After sort re-iterate your array and re-assign seq
@flix Can you verify if your original seq value will always be in order and not loose any value (like 1,2,4,5,7) in between?
@gurvinder372 for real, i have 6 index of json, and this seq is like a ranking, though the answer is right, ill try it first
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);

Comments

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

1 Comment

OP is looking to retain the seq value at the same index
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


})

Comments

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.