1

I have an array with 1000 elements in it. However, the array follows a sequence - every ten elements.e.g. [fruit,vegetables,sugars,bread,fruit,vegetables,sugars ....].

I would need to extract every fruit, vegetable and so on into different arrays, however there are 10 classes of them and I need to make ten different arrays out of this one.

What would be the most reliable approach to this problem?

Work is on JavaScript

5
  • In your 1000 element array, do you know what category they are? Commented Jan 15, 2018 at 16:07
  • show us your array and show us what you tried Commented Jan 15, 2018 at 16:07
  • Yes, I know the the categories there are ten of them which follow a sequence throughout the 100 elements. Commented Jan 15, 2018 at 16:07
  • The example of the array is written, I have achieved success in smaller array which only had 3 categories. So %3==0 to extract the third element and make a temp array, where later i extract the other element by using %2==0 from the temp array Commented Jan 15, 2018 at 16:10
  • Can you give me a 15 element example of your array? Is it a multidimensional array? Commented Jan 15, 2018 at 16:55

3 Answers 3

1

You could take an array with references to the wanted arrays and as index for the array for pusing the remainder value of the actual index and the length of the temporary array.

var array = ['fruit', 'vegetables', 'sugars', 'bread', 'fruit', 'vegetables', 'sugars', 'bread'],
    fruits = [],      // final arrays
    vegetables = [], //
    sugars = [],    //
    breads = [],   //
    temp = [fruits, vegetables, sugars, breads],
    len = temp.length;
    
array.forEach((v, i) => temp[i % len].push(v));
    
console.log(fruits);
console.log(vegetables);
console.log(sugars);
console.log(breads);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

3 Comments

The elements them self dont repeat (they are different each time) but they follow a sequence. I have 10 different categories that i need to extract from that array.*The elements are not identical*
i took only four instead of then. the content could be different. this is just an example with a smaller subset.
As written this modifies the window.length value, just pointing that out.
0

Not super elegant but it will do the job..

var a = 
 ['bread_1','fruit_1','vegetable_1','sugars_1',
  'bread_2','fruit_2','vegetable_2','sugars_2',
  'bread_3','fruit_3','vegetable_3','sugars_3'];
  
var i=0; 
a = a.reduce(function(ac, va, id, ar){
  if(i==ac.length) i=0;
  ac[i].push(va);
  i++;
  return ac;
}, [[],[],[],[]]);

console.log(a);
  

  

Comments

0

I suggest (since the pattern may vary) to create an array with categories and what elements those categories include, thus creating an object with keys identifying your categories.

The variables are not 'declared' outside the object but you can access the object keys the same way you'd have different variables:

// Simple old-style catalog as reference for your elements array.
var Categories = {
    'fruits': ['orange', 'apple', 'all other fruits…'],
    'vegetables': ['ginger', 'broccoli', 'all other vegetables…'],
    'bread': ['pizza', 'panini', 'all other breads…'],
    'sugars': ['soda', 'sugar1', '90_percent_of_products_are_sugar', 'all other sugars…']
};

// Your actual elements array.
var ElementsArray = [
    'orange',
    'broccoli',
    'pizza',
    'sugar1',
    'apple',
    'ginger',
    'panini',
    'soda'
];

// Your organized-by-category variable, declare as object so you can easily access as Array or Object (keys are the variable arrays).
var OrderedElementsArray = {};

for (element in ElementsArray)
{
    for (category in Categories)
    {
        // Check if the key is not an array an initialize it for later use of push().
        if (typeof OrderedElementsArray[category] != 'object')
        {
            OrderedElementsArray[category] = [];
        }

        // indexOf() returns -1 if no element matches an index, thus the expression `>= 0`.
        if (Categories[category].indexOf(ElementsArray[element]) >= 0)
        {
            OrderedElementsArray[category].push(ElementsArray[element]);
        }
    }
}

// Here you can access your object variables with dot notation. All your categories will be accessible either way.
console.log(OrderedElementsArray.fruits);
console.log(OrderedElementsArray.vegetables);
console.log(OrderedElementsArray.bread);
console.log(OrderedElementsArray.sugars);

// Here you can access your object variables with key notation. All your categories will be accessible either way.
console.log(OrderedElementsArray['fruits']);
console.log(OrderedElementsArray['vegetables']);
console.log(OrderedElementsArray['bread']);
console.log(OrderedElementsArray['sugars']);

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.