簡體   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