Как разделить массив по дублирующим значениям?

 const msg1 = {
      fname: 'Divyesh',
      lname: 'Patel',
    }
    const person = {
      fullname: function (city, num) {
        return this.fname + " " + this.lname + " " + city + " " + num;
      }
    }
    console.log(person.fullname.call(msg1, "Surat", "972737"));
1
задан a8hok 19 January 2019 в 19:12
поделиться

4 ответа

Вы можете использовать простой reduce , чтобы получить это:

const input=[{index:0,value:3},{index:0,value:3},{index:0,value:3},{index:1,value:3},{index:1,value:3},{index:2,value:3},{index:2,value:3}]

/* If index exists in the accumulator object, use that.
  else, create the index and point to an empty array
  push the item in context to the array
*/
const output = input.reduce((acc, v) => {
  acc[v.index] = acc[v.index] || [];
  acc[v.index].push(v);
  return acc;
}, {});

console.log(output)

Вот короткая версия:

const input=[{index:0,value:3},{index:0,value:3},{index:0,value:3},{index:1,value:3},{index:1,value:3},{index:2,value:3},{index:2,value:3}]

const output = input.reduce((a,v) => ((a[v.index] = a[v.index] || []).push(v), a), {});

console.log(output)

0
ответ дан adiga 19 January 2019 в 19:12
поделиться

Выполните простую операцию reduce:

const data = [{
    index: 0,
    value: 3
  },
  {
    index: 0,
    value: 3
  },
  {
    index: 0,
    value: 3
  },
  {
    index: 1,
    value: 3
  },
  {
    index: 1,
    value: 3
  },
  {
    index: 2,
    value: 3
  }, {
    index: 2,
    value: 3
  }
];

const modified = data.reduce((val, acc) => {
  if (acc[val.index]) {
    acc[val.index].push(val);
  } else {
    acc[val.index] = [{
      index: [val.index],
      value: [val.value]
    }];
  }
  return acc;
}, {});

console.log(modified);

0
ответ дан Jack Bashford 19 January 2019 в 19:12
поделиться

Вы можете использовать простой цикл for

const input = [
    {index: 0, value: 3},
    {index: 0, value: 3},
    {index: 0, value: 3},
    {index: 1, value: 3},
    {index: 1, value: 3},
    {index: 2, value: 3},
    {index: 2, value: 3}
];

const newArray = {}
for(var i of input) {
  if(newArray[i.index]) {
    newArray[i.index].push(i)
  } else {
    newArray[i.index] = [i]
  }
}

console.log(newArray)

0
ответ дан Skraloupak 19 January 2019 в 19:12
поделиться

Здесь у вас есть одна версия, использующая Reduce () и Object.assign () :

const input = [
    {index: 0, value: 3},
    {index: 0, value: 3},
    {index: 0, value: 3},
    {index: 1, value: 3},
    {index: 1, value: 3},
    {index: 2, value: 3},
    {index: 2, value: 3},
    {index: 0, value: 3}
];

let obj = input.reduce((res, curr) =>
{
    if (res[[curr.index]])
        res[[curr.index]].push(curr);
    else
        res = Object.assign({[curr.index]: [curr]}, res);

    return res;
}, {});

console.log(obj);
[ 116]

В предыдущем примере будут созданы группы всех объектов с одинаковым свойством index. Однако, если вы хотите разделить объекты полосами, которые имеют равные indexes, вы можете сделать это:

const input = [
    {index: 0, value: 3},
    {index: 0, value: 3},
    {index: 0, value: 3},
    {index: 1, value: 3},
    {index: 1, value: 3},
    {index: 2, value: 3},
    {index: 2, value: 3},
    {index: 0, value: 3},
    {index: 0, value: 3},
    {index: 0, value: 3},
    {index: 2, value: 3},
    {index: 2, value: 3}
];

let obj = input.reduce((res, curr) =>
{
    if (curr.index === res.last)
        res.r[res.idx - 1].push(curr);
    else
    {
        res.r = Object.assign({[res.idx]: [curr]}, res.r);
        res.last = curr.index;
        res.idx = res.idx + 1;
    }

    return res;
}, {r: {}, idx: 0, last: null});

console.log(obj.r);

0
ответ дан Shidersz 19 January 2019 в 19:12
поделиться
Другие вопросы по тегам:

Похожие вопросы: