0

I have a mybooks array, I'd like to separate it by the book's category.

var mybooks = [
    ['novel','BookName1','$20.00'],
    ['novel','BookName2','$24.00'],
    ['novel','BookName3','$34.00'],
    ['novel','BookName4','$31.00'],
    ['novel','BookName4','$38.00'],
    ['Biograph','BookName5','$28.00'],
    ['Biograph','BookName6','$48.00'],
    ['Biograph','BookName7','$50.00']
  ];

I made it this way.

 for(i=0; i<mybooks.length; i++){
        if ((mybooks[i][0]) == 'novel'){
        var books = mybooks[i];
        console.log(books);
       }
      };

console.log(books) prints

["novel", "BookName1", "$20.00"] 
["novel", "BookName2", "$24.00"]
["novel", "BookName3", "$34.00"]
["novel", "BookName4", "$31.00"] 
["novel", "BookName4", "$38.00"] 

How can I make a global variable called 'splited' equals books

1
  • 1
    Make sure to have a look at my solution as well. It might teach you some JavaScript features ;) Commented Aug 26, 2013 at 2:16

6 Answers 6

2

Make a function return books:

var makeBooks = function(mybooks, type){
  var books=[];
  for(var i=0; i<mybooks.length; i++){
      if ((mybooks[i][0]) === type){
        books.push(mybooks[i]);
      }
  };
  return books;
}

window.splitted = makeBooks(mybooks, 'novel');
Sign up to request clarification or add additional context in comments.

Comments

1
var splitted=$.grep(mybooks,function(o){return (o[0]=='novel');});

Comments

1

Well, I am not sure exactly what you want as end result, but you could do something like this:

var splitted = mybooks.reduce(function (result, book) {
    var cat = book[0];

    if (!result[cat]) {
        result[cat] = [book];
    } else {
        result[cat].push(book);
    }

    return result;
}, {});

console.log(splitted); //all books splitted by categories
console.log(splitted.novel); //array of novels
console.log(splitted.Biograph); //array of Biograph

If you simply want to gather one category at a time, you could also use the filter function.

var novels = mybooks.filter(function (book) {
    return book[0] === 'novel';
});

Comments

1

A fairly simple approach is:

var splitted = mybooks.filter(function(book) {return book[0] == 'novel';});

You could generalize it like this:

var byType = function(type) {return function(book) {return book[0] == type;};};
var bios = mybooks.filter(byType('Biograph'));

Comments

0

Are you trying to split the array by the book category? If so, it might be good to have a look at object literals instead

1 Comment

Thanks! but needs to be array for this case :-)
0

This should arrange your book array by its type:

var mybooks = [
    ['novel','BookName1','$20.00'],
    ['novel','BookName2','$24.00'],
    ['novel','BookName3','$34.00'],
    ['novel','BookName4','$31.00'],
    ['novel','BookName4','$38.00'],
    ['Biograph','BookName5','$28.00'],
    ['Biograph','BookName6','$48.00'],
    ['Biograph','BookName7','$50.00']
  ];

var library = {}

for(i=0; i<mybooks.length; i++){
    var bookType = mybooks[i][0];
    if (typeof library[bookType]=='undefined')  
       library[bookType] = [];
    library[bookType].push(mybooks[i]);
}

console.log(library);

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.