简体   繁体   English

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

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

I have an code some like this ;我有一个类似这样的代码;

 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 will grouping the object 'name' and push it into a new array if the 'times' is same.如果“时间”相同,我会将对象“名称”分组并将其推送到新数组中。 I know its like playing array.reduce, findWhere in underscore js or something bad like that, but my logic is bad.我知道这就像在下划线 js 中玩 array.reduce、findWhere 或类似的东西,但我的逻辑很糟糕。

And i will happy if you will output my code like this ;如果您像这样输出我的代码,我会很高兴;

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

how can i did it ?我怎么办? some people wanna help me ?有些人想帮助我? thx before it thx 在它之前

You can use reduce & findIndex .Use reduce to create a new array(map can also be used).您可以使用reduce & findIndex使用reduce创建一个新数组(也可以使用 map)。 Using findIndex get the index of the object from array where the times matches.使用findIndextimes匹配的数组中获取对象的索引。 If so then update the name array else create a new object and push in the accumulator array如果是,则更新名称数组,否则创建一个新对象并推入累加器数组

 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)

You can do this efficiently by creating an object keyed to the thing you want to be unique —— times in this case. -您可以通过创建键入你想成为独特的东西的对象,它有效做到这一点times在这种情况下。 For each time create an object with an array to hold your values.每次创建一个带有数组的对象来保存您的值。 At the end the Object.values() of the object will be what you want.最后,对象的Object.values()将是您想要的。 This will be more efficient that searching the list each time for a duplicate:这将比每次搜索列表中的重复项更有效:

 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)

Here's one way you could do it using Object.values and Array#reduce :这是使用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));

Alternatively, using Object.assign :或者,使用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));

Or, with 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));

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

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