0

Consider this array:

const arr = [
  {order: 1, mess:"test1"},
  {order: 2, mess:"test2"},
  {order: 2, mess:"test2"},
  {order: 2, mess:"test2"},
  {order: 3, mess:"test3"},
  {order: 4, mess:"test4"},
]

arr is always sorted by order.

I want to modify it to get:

const arr = [
  {order: 1, mess:"test1"},

  {order: 2, mess:"test2", sep:true},
  {order: 2, mess:"test2"},
  {order: 2, mess:"test2"},

  {order: 3, mess:"test3", sep:true},

  {order: 4, mess:"test4", sep:true},
]

sep must be inserted into an object if it has an order different from the preceding object.

I'm unable to write this function, even if it might be quite simple (I guess!).
At this time, I have this, but it doesn't work at all and I don't understand its behavior.

const arr = [
  {order: 1, mess: "test1"},
  {order: 2, mess: "test2"},
  {order: 2, mess: "test2"},
  {order: 2, mess: "test2"},
  {order: 3, mess: "test3"},
  {order: 4, mess: "test4"},
  ]


arr.forEach(function(value, i) {
  i = Number(i)
  const y = i + 1
  if (arr[i] !== undefined && arr[y] !== undefined) {
    if (arr[i].order === arr[y].order) arr[y].sep = true;
  }
});

console.log(arr)

Do you have any idea? Thanks

1
  • i = Number(i) is not necessary Commented Apr 8, 2022 at 11:20

6 Answers 6

3

You could map a new array and check the item and the predecessor.

const
    data = [{ order: 1, mess: "test1" }, { order: 2, mess: "test2" }, { order: 2, mess: "test2" }, { order: 2, mess: "test2" }, { order: 3, mess: "test3" }, { order: 4, mess: "test4" }],
    result = data.map((o, i, a) => i && a[i - 1].order !== o.order
        ? { ...o, sep: true }
        : o
    );

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

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

Comments

2

Try this

const arr = [
  {order: 1, mess: "test1"},
  {order: 2, mess: "test2"},
  {order: 2, mess: "test2"},
  {order: 2, mess: "test2"},
  {order: 3, mess: "test3"},
  {order: 4, mess: "test4"},
  ]

arr.forEach((obj,index)=>{
  if(index>0 && obj.order != arr[index-1]['order']){
    arr[index]['sep']=true
  }
});
console.log(arr)

2 Comments

why not arr[index-1].order no need for the bracket notation
yes, simple dot notation will also work.
0

Simplest change of existing array

const arr = [{ order: 1, mess: "test1" }, { order: 2, mess: "test2" }, { order: 2, mess: "test2" }, { order: 2, mess: "test2" }, { order: 3, mess: "test3" }, { order: 4, mess: "test4" }];

arr.forEach((value, i) => {
  if (i && arr[i-1].order !== arr[i].order) arr[i].sep = true;
});
console.log(arr)

1 Comment

Works like a charme and change the current array! Thx.
0

Changes original array:

arr.forEach((el, idx) => {
  if (idx === 0) {
    el.sep = true;
  } else {
    if (el.order !== arr[idx-1].order) {
      el.sep = true;
    }
  }
})

Comments

0

Below is another way to achieve the same result as you want.

I believe a more generalized way.

arr.forEach((obj, index) => {
  
  if(JSON.stringify(obj[index+1]) === JSON.stringify(obj[index+2])) {
    obj[index].sep = true;
  }
})

Comments

-1

You can copy the array:

let arr2 = [...arr];

Shift it (so the first item is the second one!)

arr2.shift()

Loop over both

const arr = [{ order: 1, mess: "test1" }, { order: 2, mess: "test2" }, { order: 2, mess: "test2" }, { order: 2, mess: "test2" }, { order: 3, mess: "test3" }, { order: 4, mess: "test4" }];
let arr2 = [...arr];
arr2.shift();
const result = [arr[0]];
let sep = true;
for (const [v1, v2] of [arr, arr2]) if (v1.order===v2.order) result.push(v2); else result.push({...v2, sep})
console.log(result)

1 Comment

That does not seem to work all the way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.