0

I have an array that I need to split into an array of arrays. I've seen a lot on grouping, splitting, sorting, etc... but these all create separate arrays. I need to keep the one array.

data = [
    {
      CustAccount: 'FEN000',
      JobNumber: 1026704,
      Open: 1
    },
    {
      CustAccount: 'FEN000',
      JobNumber: 1026556,
      Open: 47
    },
    {
      CustAccount: 'FEN000',
      JobNumber: 1026627,
      Open: 11
    },
    {
      CustAccount: 'FEN000',
      JobNumber: 1026627,
      Open: 33
    },
    {
      CustAccount: 'FEN000',
      JobNumber: 1026627,
      Open: 17
    },
    {
      CustAccount: 'FEN000',
      JobNumber: 1026627,
      Open: 2
    }
]

I need to create a new array that puts objects into a subarray based off the range of Open < 11, 11 - 29, 30+ using javascript.

newdata = [
    [{
      CustAccount: 'FEN000',
      JobNumber: 1026704,
      Open: 1
    },
    {
      CustAccount: 'FEN000',
      JobNumber: 1026556,
      Open: 2
    }
]
[
    {
      CustAccount: 'FEN000',
      JobNumber: 1026627,
      Open: 11
    },
    {
      CustAccount: 'FEN000',
      JobNumber: 1026627,
      Open: 17
    }
]
[
    {
      CustAccount: 'FEN000',
      JobNumber: 1026627,
      Open: 33
    },
    {
      CustAccount: 'FEN000',
      JobNumber: 1026627,
      Open: 47
    }
]
]

Any help would be appreciated

1
  • Loop over the original array and put the items in new arrays based on open. Then concatenate the arrays. Commented Sep 1, 2021 at 19:21

5 Answers 5

1

Here is your array of arrays:

var newData = [];
newData[0] = data.filter(item => item.Open <= 10);
newData[1] = data.filter(item => item.Open > 10 && item.Open < 30);
newData[2] = data.filter(item => item.Open >= 30);
Sign up to request clarification or add additional context in comments.

1 Comment

And of course this can also be written as const newData = [data.filter(({Open}) => Open <= 10), data.filter(({Open}) => Open > 10 && Open < 30), data.filter(({Open}) => Open >= 30)].
0

let data = [
    {
      CustAccount: 'FEN000',
      JobNumber: 1026704,
      Open: 1
    },
    {
      CustAccount: 'FEN000',
      JobNumber: 1026556,
      Open: 47
    },
    {
      CustAccount: 'FEN000',
      JobNumber: 1026627,
      Open: 11
    },
    {
      CustAccount: 'FEN000',
      JobNumber: 1026627,
      Open: 33
    },
    {
      CustAccount: 'FEN000',
      JobNumber: 1026627,
      Open: 17
    },
    {
      CustAccount: 'FEN000',
      JobNumber: 1026627,
      Open: 2
    }
];

const lessThan11 = [];
const lessThan29 = [];
const gt30 = [];

for (let item of data) {
   if (item.Open < 11) {
     lessThan11.push(item);
   } else if (item.Open <= 29) {
     lessThan29.push(item);   
   } else {
      gt30.push(item);
   }
}

const result= [lessThan11, lessThan29, gt30];
console.log(result);

1 Comment

Thank you Christian!
0

Iterate over all the items in data and based on open values of that item put it in different arrays. Lastly,combine them to get newdata

const arr1=[];
const arr2=[];
const arr3=[];

for(let i=0;i<data.length;i++){
 if(data[i]['Open']<11){
  arr1.push(data[i]);
 }
 else if(data[i]['Open']<30){
  arr2.push(data[i]);
 }
 else arr3.push(data[i]);
}

var newdata=[arr1,arr2,arr3];

1 Comment

Thank you! I tried the loop through on my own, but didn't think of joining all the new arrays into one... Ugh! Thanks Again!
0

Create an array with three empty arrays inside first, then push to the desired inside array based on the value of Open

data=[{CustAccount:"FEN000",JobNumber:1026704,Open:1},{CustAccount:"FEN000",JobNumber:1026556,Open:47},{CustAccount:"FEN000",JobNumber:1026627,Open:11},{CustAccount:"FEN000",JobNumber:1026627,Open:33},{CustAccount:"FEN000",JobNumber:1026627,Open:17},{CustAccount:"FEN000",JobNumber:1026627,Open:2}]

const newdata = [[],[],[]]
data.map(el =>
{
  if(el.Open < 11)
  {
    return newdata[0].push(el)
  }
  else if(el.Open < 30)
  {
    return newdata[1].push(el)
  }
  else
  {
    return newdata[2].push(el)
  }
})

console.log(newdata)

Comments

0

If we write a generic function to split an array according to which test the values pass, then we might write a function to do what you would like in this manner:

const myGroups = splitInto ([
  ({Open}) => Open < 11,
  ({Open}) => Open < 30,
  ({Open}) => Open >= 30,
])

and then just call it with myGroups (data).

You can see one implementation of that idea here:

const splitInto = (fns) => (xs) =>
  xs .reduce (
    (rss, x) => {
      const idx = fns .findIndex (fn => fn (x))
      if (idx >= 0 && idx < fns.length) {rss [idx] .push (x)}
      return rss
    },
    Array.from(fns, () => [])
  )

const myGroups = splitInto ([
  ({Open}) => Open < 11,
  ({Open}) => Open < 30,
  ({Open}) => Open >= 30,
]) 

const data = [{CustAccount: 'FEN000', JobNumber: 1026704, Open: 1}, {CustAccount: 'FEN000', JobNumber: 1026556, Open: 47}, {CustAccount: 'FEN000', JobNumber: 1026627, Open: 11}, {CustAccount: 'FEN000', JobNumber: 1026627, Open: 33}, {CustAccount: 'FEN000', JobNumber: 1026627, Open: 17}, {CustAccount: 'FEN000', JobNumber: 1026627, Open: 2}]

console .log (myGroups (data))
.as-console-wrapper {max-height: 100% !important; top: 0}

Note that if none of the tests pass, the value is skipped altogether. It would not be difficult to decide to add them to a dedicated final group or to throw an error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.