繁体   English   中英

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

[英]Push array in object if some keys are same in javascript

我有一个类似这样的代码;

 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'}
  ]; 

and push it into a new array if the is same.如果相同,我会将对象分组并将其推送到新数组中。 我知道这就像在下划线 js 中玩 array.reduce、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'] },
  ]; 

我怎么办? 有些人想帮助我? thx 在它之前

您可以使用reduce & findIndex使用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)

-您可以通过创建键入你想成为独特的东西的对象,它有效做到这一点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)

这是使用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));

或者,使用对象传播语法

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM