简体   繁体   English

Javascript:根据属性值将对象数组拆分为具有动态名称的单独数组

[英]Javascript: Split array of objects into seperate arrays with dynamic names depending on property value

I have an array containing objects. 我有一个包含对象的数组。 Now I want to slice the array to new arrays containing only those objects matching a certain property value. 现在我想将数组切片到只包含那些匹配某个属性值的对象的新数组。

Ideally the new array names should be created dynamically. 理想情况下,应该动态创建新的数组名称。

The original array looks like this: 原始数组如下所示:

specificSlotButtonArray = [
  {slotStarttime:"06:00:00", slotTimespan:1},
  {slotStarttime:"09:00:00", slotTimespan:1},
  {slotStarttime:"12:00:00", slotTimespan:2},
  {slotStarttime:"15:00:00", slotTimespan:2},
  {slotStarttime:"18:00:00", slotTimespan:3}
];

The new arrays should look like this: 新数组应如下所示:

timespan1 = [
  {slotStarttime:"06:00:00", slotTimespan:1},
  {slotStarttime:"09:00:00", slotTimespan:1}
]

timespan2 = [
  {slotStarttime:"12:00:00", slotTimespan:2},
  {slotStarttime:"15:00:00", slotTimespan:2}
]

timespan3 = [
  {slotStarttime:"18:00:00", slotTimespan:3}
]

If possible, I want to avoid javascript syntax / functions, which are not supported by IE and some other older browsers. 如果可能的话,我想避免使用IE和其他一些旧浏览器不支持的javascript语法/函数。

I already tried to work with reduce() and slice() , but did not find a solution. 我已经尝试使用reduce()slice() ,但没有找到解决方案。

You can simply achieve your desired outcome using reduce , as you can produce an object using reduce , here's an example of how you could do it. 您可以使用reduce来简单地实现所需的结果,因为您可以使用reduce生成对象,这是一个如何实现它的示例。

As you can see, it'll check that the relevant property on the object isn't null, if it is, then it's set to an empty array, after this check, it's safe to simply push the relevant values onto the array, like so. 如您所见,它将检查对象上的相关属性是否为空,如果是,则将其设置为空数组,在此检查之后,将相关值简单地推送到数组上是安全的,如所以。

 var array = [{ slotStarttime: "06:00:00", slotTimespan: 1 }, { slotStarttime: "09:00:00", slotTimespan: 1 }, { slotStarttime: "12:00:00", slotTimespan: 2 }, { slotStarttime: "15:00:00", slotTimespan: 2 }, { slotStarttime: "18:00:00", slotTimespan: 3 } ]; var newObject = array.reduce(function(obj, value) { var key = `timespan${value.slotTimespan}`; if (obj[key] == null) obj[key] = []; obj[key].push(value); return obj; }, {}); console.log(newObject); 

You could reduce the array by taking an object for the part arrays. 您可以通过获取零件阵列的对象来减少阵列。

 var specificSlotButtonArray = [{ slotStarttime: "06:00:00", slotTimespan: 1 }, { slotStarttime: "09:00:00", slotTimespan: 1 }, { slotStarttime: "12:00:00", slotTimespan: 2 }, { slotStarttime: "15:00:00", slotTimespan: 2 }, { slotStarttime: "18:00:00", slotTimespan: 3 }], timespan1 = [], timespan2 = [], timespan3 = []; specificSlotButtonArray.reduce(function (r, o) { r[o.slotTimespan].push(o); return r; }, { 1: timespan1, 2: timespan2, 3: timespan3 }); console.log(timespan1); console.log(timespan2); console.log(timespan3); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

Use a generic group by key reducer. 按键缩减器使用通用组。 I will take it from a previous answer of mine . 我将从之前的答案中接受它。 It is an elegant and simple way to generate a function that group your data by a particular key that comes as an argument. 这是一种优雅而简单的方法,可以生成一个函数,该函数通过作为参数的特定键对数据进行分组。

 const groupBy = key => (result,current) => { const item = Object.assign({},current); if (typeof result[current[key]] == 'undefined'){ result[current[key]] = [item]; }else{ result[current[key]].push(item); } return result; }; const specificSlotButtonArray = [ {slotStarttime:"06:00:00", slotTimespan:1}, {slotStarttime:"09:00:00", slotTimespan:1}, {slotStarttime:"12:00:00", slotTimespan:2}, {slotStarttime:"15:00:00", slotTimespan:2}, {slotStarttime:"18:00:00", slotTimespan:3} ]; const timespan = specificSlotButtonArray.reduce(groupBy('slotTimespan'),{}); console.log(timespan); 

The following soluce iterates once on specificSlotButtonArray using Array.reduce . 以下解决方案使用Array.reducespecificSlotButtonArray上迭代一次。 This soluce will adapt to any number of slotTimespan . 这个解决方案将适应任何数量的slotTimespan

 const specificSlotButtonArray = [{ slotStarttime: '06:00:00', slotTimespan: 1, }, { slotStarttime: '09:00:00', slotTimespan: 1, }, { slotStarttime: '12:00:00', slotTimespan: 2, }, { slotStarttime: '15:00:00', slotTimespan: 2, }, { slotStarttime: '18:00:00', slotTimespan: 3, }, ]; // Loop through the array const splitted = specificSlotButtonArray.reduce((tmp, x) => { // Look if we got already an array having the slotTimespan of the current // item to treat const match = tmp.find(y => y.some(z => z.slotTimespan === x.slotTimespan)); // If we have such array, push the data into it if (match) { match.push(x); } else { // If we don't create a new array tmp.push([x]); } return tmp; }, []); console.log(splitted); 

If you want to deals with the array straight after the Array.reduce you can use destructuration : 如果你想在Array.reduce之后直接处理数组,你可以使用destruct Array.reduce

const [
  timespan1,
  timespan2,
  timespan3
] = specificSlotButtonArray.reduce((tmp, x) => {

You can use this function to create separate arrays grouped by slotTimespan , 您可以使用此函数创建按slotTimespan分组的单独数组,

  specificSlotButtonArray = [ {slotStarttime:"06:00:00", slotTimespan:1}, {slotStarttime:"09:00:00", slotTimespan:1}, {slotStarttime:"12:00:00", slotTimespan:2}, {slotStarttime:"15:00:00", slotTimespan:2}, {slotStarttime:"18:00:00", slotTimespan:3} ]; function groupBy(arr, property) { return arr.reduce(function(memo, x) { if (!memo[x[property]]) { memo[x[property]] = []; } memo[x[property]].push(x); return memo; }, {}); } console.log(groupBy(specificSlotButtonArray, "slotTimespan")); 

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

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