如果某些键在javascript中相同,则在对象中推送数组

时间:2018-10-22 18:30:20

标签: javascript arrays sorting object

我有这样的代码;

 var data = [
 {'times':'07:50','name':'bojes'},
 {'times':'07:50','name':'unknw1q2q'},
 {'times':'09:50','name':'rafik'},
 {'times':'11:50','name':'adit'},
 {'times':'15:50','name':'adit'},
 {'times':'15:50','name':'adrok'}
  ]; 

i将对对象'name'进行分组,如果'times'相同,则将其推入新数组。我知道这就像玩array.reduce,在下划线js中找到findWhere或类似的东西一样,但是我的逻辑是不好的。

如果您能像这样输出我的代码,我会很高兴;

var newData = [
 {'times':'07:50','name': ['bojes','unknw1q2q'] },
 {'times':'09:50','name': ['rafik'] },
 {'times':'11:50','name': ['adit'] },
 {'times':'15:50','name': ['adit','adrok'] },
  ]; 

我该怎么做? 有些人想帮助我吗?谢谢

3 个答案:

答案 0 :(得分:0)

您可以使用reducefindIndex。使用reduce可以创建一个新数组(也可以使用map)。使用findIndextimes匹配的数组中获取对象的索引。如果是这样,则更新名称数组,否则创建一个新对象并推入累加器数组

var data = [{
    'times': '07:50',
    'name': 'bojes'
  },
  {
    'times': '07:50',
    'name': 'unknw1q2q'
  },
  {
    'times': '09:50',
    'name': 'rafik'
  },
  {
    'times': '11:50',
    'name': 'adit'
  },
  {
    'times': '15:50',
    'name': 'adit'
  },
  {
    'times': '15:50',
    'name': 'adrok'
  }
];

let k = data.reduce(function(acc, curr) {
  // check if the times is present

  let findIndex = acc.findIndex(function(item) {
    return item.times === curr.times
  })

  if (findIndex == -1) {
    let obj = Object.assign({}, {
      times: curr.times,
      name: [curr.name]
    })
    acc.push(obj)
  } else {
    acc[findIndex].name.push(curr.name)
  }

  return acc;
}, []);

console.log(k)

答案 1 :(得分:0)

在这种情况下,您可以通过创建一个对象来锁定要成为唯一对象的对象来高效地进行操作-times。每次创建一个带有数组的对象来保存您的值。最后,对象的Object.values()将是您想要的。这样会比每次在列表中搜索重复项更为有效:

var data = [{'times':'07:50','name':'bojes'},{'times':'07:50','name':'unknw1q2q'},{'times':'09:50','name':'rafik'},{'times':'11:50','name':'adit'},{'times':'15:50','name':'adit'},{'times':'15:50','name':'adrok'}];

let res = Object.values(data.reduce((obj, curr) => {
    // if we haven't seen the time before make a new entry
    if(!obj[curr.times]) obj[curr.times] = {times: curr.times, name: []}
    // push the current name into the current time entry
    obj[curr.times]['name'].push(curr.name)
    return obj
}, {}))
console.log(res)

答案 2 :(得分:0)

这是使用Object.valuesArray#reduce的一种方式:

const data = [
  {'times':'07:50','name':'bojes'},
  {'times':'07:50','name':'unknw1q2q'},
  {'times':'09:50','name':'rafik'},
  {'times':'11:50','name':'adit'},
  {'times':'15:50','name':'adit'},
  {'times':'15:50','name':'adrok'}
];

const groupByTimes = data => Object.values(data.reduce((data, {times, name}) => {
  if (data[times]) data[times].name.push(name);
  else data[times] = {times, name: [name]};
  return data;
}, {}));

console.log(groupByTimes(data));

或者,使用Object.assign

const data = [
  {'times':'07:50','name':'bojes'},
  {'times':'07:50','name':'unknw1q2q'},
  {'times':'09:50','name':'rafik'},
  {'times':'11:50','name':'adit'},
  {'times':'15:50','name':'adit'},
  {'times':'15:50','name':'adrok'}
];

const groupByTimes = data => Object.values(
  data.reduce((data, {times, name}) => Object.assign({}, data, {
    [times]: data[times]
      ? {times, name: [...data[times].name, name]}
      : {times, name: [name]}
  }), {})
);

console.log(groupByTimes(data));

或者,用object spread syntax

const data = [
  {'times':'07:50','name':'bojes'},
  {'times':'07:50','name':'unknw1q2q'},
  {'times':'09:50','name':'rafik'},
  {'times':'11:50','name':'adit'},
  {'times':'15:50','name':'adit'},
  {'times':'15:50','name':'adrok'}
];

const groupByTimes = data => Object.values(
  data.reduce((data, {times, name}) => ({
    ...data,
    [times]: data[times]
      ? {times, name: [...data[times].name, name]}
      : {times, name: [name]}
  }), {})
);

console.log(groupByTimes(data));