user6279105
user6279105

Reputation: 45

How do I create an X amount of separate arrays based on certain conditions I specify?

Hello I'm new to JavaScript and I am trying to figure out how to create separate arrays based on the value of categories I have and the arrays to be named the name of the category.

I have an object contains my all my data. One of the keys of the object is category.

Here's what I have so far:

class Image {
    constructor(topHeading,bottomHeading,description,thumbNail,lrgImg,category, all){
        this.topHeading = topHeading;
        this.bottomHeading = bottomHeading;
        this.description = description;
        this.thumbNail = thumbNail;
        this.lrgImg = lrgImg;
        this.category = category;
        this.all = all;
    }
}

let counter= 0;
let allImages = {};
let allData = Object.entries(allImages);
let allCategoriesArr= [];
let filteredCategoriesArr = [];

const createImage = function(topHeading,bottomHeading,description,thumbnail,lrgImg,category,all){
    allImages[counter+1] = new Image(topHeading,bottomHeading,description,thumbnail,lrgImg,category,all)
    counter += 1;
}

createImage('1', 'birthday cake', 'description',  'test', 'test/path','birthday','all');

createImage('2', 'birthday cake', 'description',  'test', 'test/path','birthday','all');

createImage('3', 'birthday cake', 'description',  'test', 'test/path','food','all');

createImage('4', 'birthday cake', 'description ',  'tes', 'test/path','food','all');

createImage('5', 'birthday cake', 'decription',  'test', 'test/path','shoe','all');


for (const data of allData) { allCategoriesArr.push(data[1].category)};


allCategoriesArr.filter((val,i, arr) => { if (arr.indexOf(val)=== i) filteredCategoriesArr.push(val) })



for (const data of allData) {    
    for (let i=0; i < filteredCategoriesArr.length; i++){
        if(data[1].category === filteredCategoriesArr[i]){
          
          console.log(data)
          
        }
    }
}

Upvotes: 0

Views: 44

Answers (1)

Alireza Ahmadi
Alireza Ahmadi

Reputation: 9923

First get your category by:

let allCategoriesArr = allImages.map(t => t.category);

Then create array of key value object for your keys (birthday,..) and values based on your key:

var newKeyValueArray = {};

allCategoriesArr.forEach(t => {
    newKeyValueArray[t] = allImages.filter(x => x.category == t);
});

Try this one:

allImages = [];

class Image {
    constructor(topHeading, bottomHeading, description, thumbNail, lrgImg, category, all) {
        this.topHeading = topHeading;
        this.bottomHeading = bottomHeading;
        this.description = description;
        this.thumbNail = thumbNail;
        this.lrgImg = lrgImg;
        this.category = category;
        this.all = all;
    }
}

const createImage = function (topHeading, bottomHeading, description, thumbnail, lrgImg, category, all) {
    allImages.push(new Image(topHeading, bottomHeading, description, thumbnail, lrgImg, category, all))
}

var img1 = "img1";
var img1Lrg = "img1Lrg";
img2Lrg = "img2Lrg";

createImage('title', 'birthday cake', 'description', img1, img1Lrg, 'birthday', 'all');

createImage('title', 'birthday cake', 'description', img1, img2Lrg, 'food', 'all');

createImage('title', 'birthday cake', 'description', img1, img2Lrg, 'shoe', 'all');
createImage('title', 'birthday cake', 'description', img1, img2Lrg, 'shoe', 'all');

let allCategoriesArr = allImages.map(t => t.category);

var newKeyValueArray = {};

allCategoriesArr.forEach(t => {
    newKeyValueArray[t] = allImages.filter(x => x.category == t);
});
console.log(newKeyValueArray);

Upvotes: 1

Related Questions