0

I'm looking for a way of sorting a large data set similar to the one I provide bellow, without using large loops. I'm looking to sort the data of '439190' into multiple arrays containing only 1 of the 'colorid' in each array. I've been looking through all the filtering, mapping and reducing methods with no success, if anyone could point me in the right direction that would be great.

Raw Json output:

{ '439190':
   [ { assetid: '9354886749',
       classid: 439190,
       colorid: '1650467837',
       category: 'student',
       favouritecolor: '439190-Red' },
     { assetid: '9354886743',
       classid: 439190,
       colorid: '1650467837',
       category: 'student',
       favouritecolor: '439190-Red' },
     { assetid: '9354886777',
       classid: 439190,
       colorid: '1650467837',
       category: 'student',
       favouritecolor: '439190-Red' },
     { assetid: '9354886726',
       classid: 439190,
       colorid: '1650746832',
       category: 'student',
       favouritecolor: '439190-Blue' },
     { assetid: '9354886755',
       classid: 439190,
       colorid: '1650746832',
       category: 'student',
       favouritecolor: '439190-Blue' },
     { assetid: '9354886753',
       classid: 439190,
       colorid: '1650746832',
       category: 'student',
       favouritecolor: '439190-Blue' },
     { assetid: '9354869964',
       classid: 439190,
       colorid: '1649236744',
       category: 'student',
       favouritecolor: '439190-Green' },
     { assetid: '9354869970',
       classid: 439190,
       colorid: '1649236744',
       category: 'student',
       favouritecolor: '439190-Green' },
     { assetid: '9354869963',
       classid: 439190,
       colorid: '1649236744',
       category: 'student',
       favouritecolor: '439190-Green' },
     { assetid: '9354886738',
       classid: 439190,
       colorid: '1651234125',
       category: 'student',
       favouritecolor: '439190-Yellow' },
     { assetid: '9354886773',
       classid: 439190,
       colorid: '1651234125',
       category: 'student',
       favouritecolor: '439190-Yellow' },
     { assetid: '9354886739',
       classid: 439190,
       colorid: '1651234125',
       category: 'student',
       favouritecolor: '439190-Yellow' },
     { assetid: '9354869966',
       classid: 439190,
       colorid: '1651726369',
       category: 'student',
       favouritecolor: '439190-Pink' },
     { assetid: '9354886767',
       classid: 439190,
       colorid: '1651726369',
       category: 'student',
       favouritecolor: '439190-Pink' },
     { assetid: '9354886727',
       classid: 439190,
       colorid: '1651726369',
       category: 'student',
       favouritecolor: '439190-Pink' },
     { assetid: '9354886763',
       classid: 439190,
       colorid: '1642929857',
       category: 'student',
       favouritecolor: '439190-Orange' },
     { assetid: '9354886722',
       classid: 439190,
       colorid: '1642929857',
       category: 'student',
       favouritecolor: '439190-Orange' },
     { assetid: '9354886762',
       classid: 439190,
       colorid: '1642929857',
       category: 'student',
       favouritecolor: '439190-Orange' } ],

Intended output (rough but hopefully you get the idea):

{ '439190':
   [ [ { assetid: '9354886749',
         classid: 439190,
         colorid: '1650467837',
         category: 'student',
         favouritecolor: '439190-Red' },
       { assetid: '9354886726',
         classid: 439190,
         colorid: '1650746832',
         category: 'student',
         favouritecolor: '439190-Blue' },
       { assetid: '9354869964',
         classid: 439190,
         colorid: '1649236744',
         category: 'student',
         favouritecolor: '439190-Green' },
       { assetid: '9354886738',
         classid: 439190,
         colorid: '1651234125',
         category: 'student',
         favouritecolor: '439190-Yellow' },
       { assetid: '9354869966',
         classid: 439190,
         colorid: '1651726369',
         category: 'student',
         favouritecolor: '439190-Pink' },
       { assetid: '9354886763',
         classid: 439190,
         colorid: '1642929857',
         category: 'student',
         favouritecolor: '439190-Orange' } ],

     [ { assetid: '9354886743',
         classid: 439190,
         colorid: '1650467837',
         category: 'student',
         favouritecolor: '439190-Red' },
       { assetid: '9354886755',
         classid: 439190,
         colorid: '1650746832',
         category: 'student',
         favouritecolor: '439190-Blue' },
       { assetid: '9354869970',
         classid: 439190,
         colorid: '1649236744',
         category: 'student',
         favouritecolor: '439190-Green' },
       { assetid: '9354886773',
         classid: 439190,
         colorid: '1651234125',
         category: 'student',
         favouritecolor: '439190-Yellow' },
       { assetid: '9354886767',
         classid: 439190,
         colorid: '1651726369',
         category: 'student',
         favouritecolor: '439190-Pink' },
       { assetid: '9354886722',
         classid: 439190,
         colorid: '1642929857',
         category: 'student',
         favouritecolor: '439190-Orange' } ],

     [ { assetid: '9354886777',
         classid: 439190,
         colorid: '1650467837',
         category: 'student',
         favouritecolor: '439190-Red' },
       { assetid: '9354886753',
         classid: 439190,
         colorid: '1650746832',
         category: 'student',
         favouritecolor: '439190-Blue' },
       { assetid: '9354869963',
         classid: 439190,
         colorid: '1649236744',
         category: 'student',
         favouritecolor: '439190-Green' },
       { assetid: '9354886739',
         classid: 439190,
         colorid: '1651234125',
         category: 'student',
         favouritecolor: '439190-Yellow' },
       { assetid: '9354886727',
         classid: 439190,
         colorid: '1651726369',
         category: 'student',
         favouritecolor: '439190-Pink' },
       { assetid: '9354886762',
         classid: 439190,
         colorid: '1642929857',
         category: 'student',
         favouritecolor: '439190-Orange' } ] ],

My current code:

for(i in json){
  test = Array.from(new Set(json[i].map(item => item.colorid)))
  .map( colorid=> {
    return {
      "assetid":json[i].find(s => s.colorid=== colorid).assetid,
      "classid":json[i].find(s => s.colorid=== colorid).classid,
      "colorid":colorid,
      "category":json[i].find(s => s.colorid=== colorid).category,
      "favouritecolor":json[i].find(s => s.colorid=== colorid).favouritecolor
    };
  });
  console.log(test);
}

it does filter the results to only contain unique colors and it retains all the key data, though it only returns one array. I've got to this point but I have been unsuccessful in finding an efficient way (not using multiple in-bedded loops) to get to the expected output shown above. Any help in improving my code or pointing me in the right direction would be appreciated :)

2
  • Will require a number of different loops to accomplish this. Show what you have tried and where you are stuck. Start by grouping the arrays of same color in an object or Map Commented Dec 11, 2018 at 14:25
  • I understand some loops would be required, though I want to use as little as possible to keep the function efficient. I will edit the question to include some code. Commented Dec 12, 2018 at 0:53

1 Answer 1

3

I just threw this together to give you some pointers but something like this should help. You need to create a reduce function which has an array of arrays as an accumulator. Then check each array to see if an matching element is present and add it to the first array which isn't. If the element is present in every array then push a new one.

const array = [1, 2, 3, 4, 6, 4, 5, 6, 4];

const listReducer = (accumulator, currentValue) => {
    let pushed = false;
    accumulator.forEach((list) => {
        if (!list.includes(currentValue)) {
            list.push(currentValue);
            pushed = true;
        }
    });
    if (!pushed) {
        accumulator.push([currentValue]);
    }
    return accumulator;
}

const startingValue = [];

console.log(array.reduce(listReducer, startingValue));

Sign up to request clarification or add additional context in comments.

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.